在安卓中使用Bundle在Activity之间传递数据
来源:互联网 发布:登陆微博显示网络超时 编辑:程序博客网 时间:2024/05/17 04:56
此文主要还是针对刚开始接触的小白,希望对你们有点帮助吧
第一个Activity
package com.example.jsontext;import android.os.Bundle;import android.app.Activity;import android.content.Intent;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;public class MainActivity extends Activity {//两个输入文本框,一个按钮EditText tvName;EditText tvAge;Button btn;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//findview tvName=(EditText) findViewById(R.id.name);tvAge=(EditText) findViewById(R.id.age);btn=(Button) findViewById(R.id.button1);//设置内部类的监听btn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View view) {//获取输入的值,并toStringString name=tvName.getText().toString();String age=tvAge.getText().toString();//使用Bundle<KEY,String>---->KEY:标记 String:值Bundle bundle=new Bundle();bundle.putString("NAME", name);bundle.putString("AGE", age);//打开ActivityIntent intent =new Intent(MainActivity.this,SecondActivity.class);//讲封装好的bundle传入intent,就完成了数据的跨Activity传递intent.putExtras(bundle);startActivity(intent);}});}}
第一个布局:
<pre name="code" class="html"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <EditText android:id="@+id/name" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:hint="请输入名字:" /> <EditText android:id="@+id/age" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/name" android:gravity="center" android:hint="请输入年龄:" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/age" android:layout_centerHorizontal="true" android:layout_marginTop="36dp" android:text="Button" /></RelativeLayout>
</pre><p></p><p>第二个Activity:</p><p><pre name="code" class="html">package com.example.jsontext;import android.os.Bundle;import android.app.Activity;import android.widget.TextView;public class SecondActivity extends Activity {Entity entity;TextView tvMessage;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_second);//findviewtvMessage=(TextView) findViewById(R.id.tv_message);//获得bundle对象后,把数据getgetExtras出来//getIntent()-->获取相应的传递的数据Bundle bundle=getIntent().getExtras();//然后就是根据标记(KEY)取出值就ok String name=bundle.getString("NAME");String age=bundle.getString("AGE");//最后一步就是显示了tvMessage.setText("姓名:"+name+",年龄:"+age);/** * 这个方法还用很多延伸,希望对大家有帮助 * Avater 2015/12/24 */}}
布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".SecondActivity" > <TextView android:id="@+id/tv_message" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /></RelativeLayout>
0 0
- 使用Bundle在Activity之间传递数据
- 使用Bundle在Activity之间传递数据
- 使用bundle在Activity之间传递数据
- 在安卓中使用Bundle在Activity之间传递数据
- 使用bundle在多个activity之间传递数据
- 使用Bundle在Activity之间进行数据传递
- 使用Bundle在Activity之间传递信息
- 使用Bundle在Activity间传递数据
- 使用Bundle在Activity间传递数据
- 使用 Bundle在Activity间传递数据
- 使用Bundle在Activity间传递数据
- 使用bundle在activity间传递数据
- 使用Bundle在fragment之间传递数据
- 使用Bundle在Activity之间交换数据
- 使用Bundle在Activity之间交换数据
- 使用Bundle在Activity之间交换数据
- 使用Bundle在Activity之间交换数据
- 使用Bundle在Activity之间交换数据
- Linux ALSA声卡驱动之七:ASoC架构中的Codec
- mybatis 注解
- mysql 批量更新 update foreach
- android voip你选哪一个 SipDroid,IMSDroid,CSipsimple,Linphone,webrtc?
- 细说六度分隔与相关数学模型
- 在安卓中使用Bundle在Activity之间传递数据
- python05- 文件读写
- iOS微信支付[WXApi sendReq:req]调用小技巧
- android 创键桌面
- Linux ALSA声卡驱动之八:ASoC架构中的Platform
- 用base64编码的字符串形式上传图片,并实时更新上传的进度条
- 日期
- Nginx之proxy_redirect详解
- python基础----函数参数