使用Bundle在Activity之间交换数据

来源:互联网 发布:铜镀铬和不锈钢 知乎 编辑:程序博客网 时间:2024/05/17 07:11

当一个Activity启动另一个Activity时,常常会有一些数据需要传过去,这时,这两个Activity之间就需要一个信使了;Intent,我们只需要将交换的数据放入Intent中就可以了.

Intent提供了多个重载的方法来"携带"额外的数据:

(1)putExtras(Bundle data):向Intent中放入需要"携带"的数据包.

(2)Bundle getExtras():取出Intent中所"携带"的数据包.

(3)putExtra(String name,Xxx value):向Intent中按key-value对的形式存入数据

(4)getXxxExtra(String name):从Intent中按key取出指定类型的数据.

上面方法中的Bundle就是一个简单的数据携带包,该Bundle对象包含了多个方法来存入数据.

(5)putXxx(String key,Xxx data):向Bundle中放入Int,Long等各种类型的数据.

(6)putSerializable(String key,Serializable data):向Bundle中放入一个可序列化的对象.

(7)getXxx(String key):从Bundle中取出Int,Long等各种类型的数据.

(8)getSerializable(String key,Serializable data):从Bundle中取出一个可序列化的对象.

下面呢,就用代码实例应用来介绍两个Activity之间如何通过Bundle交换数据.

这个实例有两个Acttivity,其中第一个Activity用于收集用户的输入信息,当用户单击该Activity的"注册"按钮时,应用进入第二个Activity,第二个Activity将会获取第一个Activity中的数据.

下面第一个Activity的界面布局文件:

<?xml version="1.0" encoding="utf-8"?><TableLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    >    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="请输入您的注册信息"        android:textSize="20sp"/>    <TableRow>        <TextView            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="用户名:"            android:textSize="16sp"/>        <!--定义一个EditText,用于收集用户的账号-->        <EditText            android:id="@+id/name"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:hint="请输入你的注册名"            android:selectAllOnFocus="true"/>    </TableRow>    <TableRow>        <TextView            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="密码:"            android:textSize="16sp"/>        <EditText            android:id="@+id/passwd"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:password="true"            android:selectAllOnFocus="true"/>    </TableRow>    <TableRow>        <TextView            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="性别:"            android:textSize="16sp"/>        <!--定义一组单选按钮,用于收集用户注册的性别-->        <RadioGroup            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:orientation="horizontal">            <RadioButton                android:id="@+id/male"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text=""                android:textSize="16sp"/>            <RadioButton                android:id="@+id/female"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text=""                android:textSize="16sp"/>        </RadioGroup>    </TableRow>    <Button        android:id="@+id/bn"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="注册"        android:textSize="16sp"/></TableLayout>
该界面布局对应的Java类代码如下:

public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        Button bn = (Button) findViewById(R.id.bn);        bn.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                EditText name = (EditText) findViewById(R.id.name);                EditText passwd = (EditText) findViewById(R.id.passwd);                RadioButton male = (RadioButton) findViewById(R.id.male);                String gender = male.isChecked() ? "" : "";                Person p = new Person(name.getText().toString(),passwd.getText().toString(),gender);                //创建一个Bundle对象                Bundle data = new Bundle();                data.putSerializable("person",p);                //创建一个Intent                Intent intent = new Intent(MainActivity.this,ResultActivity.class);                intent.putExtras(data);                //启动intent对应的Acticity                startActivity(intent);            }        });    }}
上面根据用户输入创建了一个Person对象,Person类只是一个简单的DTO对象,该Person类实现了java.io.Serializable接口,因此Person对象是可序列化的.

上面程序创建了一个Bundle对象,并调用putSerializable("person",p)将Person对象放入该Bundle中,然后再使用Intent来携带这个Bundle,这样既可将Person对象传入第二个Activity.

下面是ResultActivity的界面布局文件:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="match_parent"    android:layout_height="match_parent">    <!--定义三个TextView,用于显示用户输入的数据-->    <TextView        android:id="@+id/name"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:textSize="18sp"/>    <TextView        android:id="@+id/passwd"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:textSize="18sp"/>    <TextView        android:id="@+id/gender"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:textSize="18sp"/></LinearLayout>
这个Activity将从Bundle中取出前一个Activity传过来的数据,并将它们显示出来,java代码如下:

public class ResultActivity extends AppCompatActivity {    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_result);        TextView name = (TextView) findViewById(R.id.name);        TextView passwd = (TextView) findViewById(R.id.passwd);        TextView gender = (TextView) findViewById(R.id.gender);        //获取启动该ActivityIntent        Intent intent = getIntent();        //直接通过Intent取出它所携带的Bundle数据包中的数据        Person p = (Person) intent.getSerializableExtra("person");        name.setText("您的用户名为:" + p.getName());        passwd.setText("您的密码是:" + p.getPasswd());        gender.setText("您的性别为:" + p.getGender());    }}
这样就完成了数据传递,但小细节不要忘了啊,为Activity配置清单文件

下面就看一下效果图了,注册页面:

上面就是MainActivity的效果图,在此信息填写完整后,点击注册按钮,就会通过Intent来携带Bundle,将Person对象传入ResultActivity中,在ResultActivity中通过Intent取出这个数据,效果图如下:

这样通过点击按钮,就完成了Inten携带Bundle中数据的传递.

0 0
原创粉丝点击