使用Bundle在Activity之间交换数据

来源:互联网 发布:淘宝特步鞋 编辑:程序博客网 时间:2024/05/16 18:03

当一个Activity启动另一个Activity时,常常会有一些数据需要传过去。其中,两个Activity之间存在一个”信使“:Intent,因此我们主要讲需要交换的数据放入Intent中即可。

Intent提供了多个重载的方法来”携带“额外的数据包。
putExtras(Bundle data):向Intent中放入需要”携带“的数据包。
Bundle getExtras():取出Intent中所”携带“的数据包
putExtra(String name, Xxx value):向Intent中按key-value对的形式存入数据。
getXxxExtra(String name):向Intent中按key取出指定类型的数据。

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

putXxx(String key, Xxx data):向Bundle中放入Int、Long等各种类型的数据。
putSerializable(String key , Serializable data):向Bundle中放入一个可序列化的对象。

为了取出Bundle数据携带包里的数据,Bundle提供如下方法:
getXxx(String key) : 从Bundle中取出Int、Long等各种类型的数据。
getSerializable(String key, Serializable data):从Bundle中取出一个可序列化的对象。

下面是一个 ”通过第一个Activity的填写注册信息、在第二个Activity显示注册信息 “的实例

首先是第一个Activity的布局代码:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    android:gravity="center_horizontal"    >    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="请输入你的注册信息"        android:textSize="20sp"/>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:gravity="center_horizontal"        android:orientation="horizontal">        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="用户名:"            android:textSize="16sp"/>        <EditText            android:id="@+id/name"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:width="200px"            android:hint="name"            android:selectAllOnFocus="true"/>    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:gravity="center_horizontal"        android:orientation="horizontal">        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="密码:"                       android:textSize="16sp"/>        <EditText            android:id="@+id/passwd"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:width="200px"            android:password="true"            android:selectAllOnFocus="true"/>    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal"        android:gravity="center_horizontal"        >        <TextView             android:layout_width="wrap_content"            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>    </LinearLayout>    <Button         android:id="@+id/bn"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="注册"        android:textSize="16sp"/></LinearLayout>

该界面对应的Java类代码如下:

public class MainActivity extends ActionBarActivity {    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        Button bn = (Button) findViewById(R.id.bn);        bn.setOnClickListener(new OnClickListener() {            public void onClick(View v) {                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 data = new Bundle();                data.putSerializable("person", p);                Intent intent = new Intent(MainActivity.this, ResultActivity.class);                intent.putExtras(data);                startActivity(intent);            }        });    }}

第二个显示信息布局文件:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">    <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"/>    <Button         android:id="@+id/back"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="返回"/></LinearLayout>

第二个界面布局的Java类代码如下:

package com.example.menutest;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.*;public class ResultActivity extends Activity {    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.result);        TextView name = (TextView) findViewById(R.id.name);        TextView passwd = (TextView) findViewById(R.id.passwd);        TextView gender = (TextView) findViewById(R.id.gender);        Button back = (Button) findViewById(R.id.back);        Intent intent = getIntent();        Person p = (Person) intent.getSerializableExtra("person");        name.setText("你的用户名:"+p.getName());        passwd.setText("你的密码为:"+ p.getPasswd());        gender.setText("你的性别为:" + p.getGender());        back.setOnClickListener(new OnClickListener() {            public void onClick(View source) {                Intent intent = new Intent(ResultActivity.this, MainActivity.class);                startActivity(intent);                finish();            }        });    }}

其中用到的Person对象Java代码:

package com.example.menutest;import java.io.Serializable;public class Person implements Serializable{    private Integer id;    private String name;    private String passwd;    private String gender;    public Person(String name, String passwd, String gender) {        this.name = name;        this.passwd = passwd;        this.gender = gender;    }    public Integer getId() {        return id;    }    public void setId(Integer id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getPasswd() {        return passwd;    }    public void setPasswd(String passwd) {        this.passwd = passwd;    }    public String getGender() {        return gender;    }    public void setGender(String gender) {        this.gender = gender;    }}

运行结果如下:
这里写图片描述
这里写图片描述

0 0
原创粉丝点击