Android Intent传递对象的两种方法(Serializable,Parcelable)举例

时间:2022-01-12 22:40:10

本文主要是介绍Serializable,Parcelable的使用。

参考文章:http://my.oschina.net/xsjayz/blog/76906

http://blog.csdn.net/Android_Tutor/article/details/5740845

使用Bundle在Activity之间交换数据:

  1)、Intent:主要通过Intent这个信使,将需要交换的数据放入即可。Intent提供了方法用于携带数据,如:

            putExtras(Bundle data):向Intent中放入需要携带的数据;

  2)、Bundle:就是一个简单的数据包,该Bundle对象包含了多个方法来存入、取出数据,有:

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

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

            getXxx(String key):从Bundle取出Int、Long等各种类型的数据;

            getSerializable(String key):从Bundle取出一个可序列化的对象。

实例一: 注册用户信息

项目简介:程序包含两个Activity,一个给用户填写信息,另一个显示注册结果。

1.RegisterActivity.java

public class RegisterActivity extends Activity {

private Button regButton;
private EditText nameEdit;
private EditText passwdEdit;
private RadioButton maleButton;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

regButton = (Button) findViewById(R.id.register_now);
nameEdit = (EditText) findViewById(R.id.name_edit);
passwdEdit = (EditText) findViewById(R.id.password_edit);
maleButton = (RadioButton) findViewById(R.id.male_btn);

regButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {

String name = nameEdit.getText().toString();
String passwd = passwdEdit.getText().toString();
String gender = maleButton.isChecked() ? "男" : "女";

// 如果输入信息不完整,则不允许注册。
if (name.equals("") || passwd.equals("")) {
Toast.makeText(RegisterActivity.this, "请填写完整的信息!",Toast.LENGTH_SHORT)
.show();
} else {
// 创建Person对象,一个可序列化的对象。
Person person = new Person(name, passwd, gender);

Bundle bundle = new Bundle();
bundle.putSerializable("person", person);

Intent intent = new Intent(RegisterActivity.this,
ResultActivity.class);
intent.putExtras(bundle);
// 启动另一个Activity。
startActivity(intent);
}
}
});
}
}

2.ResultActivity.java

public class ResultActivity extends Activity {

private TextView nameText;
private TextView passwdText;
private TextView genderText;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.result);

nameText = (TextView) findViewById(R.id.name_text);
passwdText = (TextView) findViewById(R.id.passwd_text);
genderText = (TextView) findViewById(R.id.gender_text);

// 获取启动该ResultActivity的Intent
Intent intent = getIntent();
// 获取该Intent所携带的数据
Bundle bundle = intent.getExtras();
// 从bundle数据包中取出数据
Person person = (Person) bundle.getSerializable("person");

// 显示注册结果
nameText.setText("您的帐号:" + person.getName());
passwdText.setText("您的密码:" + person.getPasswd());
genderText.setText("您的性别:" + person.getGender());
}
}

3.Person.java

public class Person implements Serializable {

private static final long serialVersionUID = 1L;
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 String getName() {
return name;
}

public String getPasswd() {
return passwd;
}

public String getGender() {
return gender;
}
}

4.布局文件(1):main.xml

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="请输入您的注册信息"
android:textSize="20sp" />

<TableRow>

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="用户名"
android:textSize="16sp" />

<EditText
android:id="@+id/name_edit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="请输入用户名"
android:selectAllOnFocus="true" />
</TableRow>

<TableRow>

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="密码"
android:textSize="16sp" />

<EditText
android:id="@+id/password_edit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="请输入密码"
android:password="true"
android:selectAllOnFocus="true" />
</TableRow>

<TableRow>

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="性别"
android:textSize="16sp" />

<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<RadioButton
android:id="@+id/male_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男"
android:checked="true"
android:textSize="16sp" />

<RadioButton
android:id="@+id/female_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女"
android:textSize="16sp" />
</RadioGroup>
</TableRow>

<Button
android:id="@+id/register_now"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="注册"
android:textSize="16sp" />

</TableLayout>

布局文件(2):result.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
android:id="@+id/name_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="18sp" />

<TextView
android:id="@+id/passwd_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="18sp" />

<TextView
android:id="@+id/gender_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="18sp" />

</LinearLayout>

5.配置文件AndroidManifest.xml注册Activity组件


实例二:主要是介绍Serializable,Parcelable的具体应用

1.ObjectTranDemo.java

public class ObjectTranDemo extends Activity implements OnClickListener {  

private Button sButton,pButton;
public final static String SER_KEY = "ser";
public final static String PAR_KEY = "par";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setupViews();
}

//设置监听
public void setupViews(){
sButton = (Button)findViewById(R.id.button1);
pButton = (Button)findViewById(R.id.button2);
sButton.setOnClickListener(this);
pButton.setOnClickListener(this);
}
//Serializeable传递对象的方法
public void SerializeMethod(){
Person mPerson = new Person();
mPerson.setName("lucky");
mPerson.setAge(25);
Intent mIntent = new Intent(this,ObjectTranDemo1.class);
Bundle mBundle = new Bundle();
mBundle.putSerializable(SER_KEY,mPerson);
mIntent.putExtras(mBundle);

startActivity(mIntent);
}
//Pacelable传递对象方法
public void PacelableMethod(){
Book mBook = new Book();
mBook.setBookName("Android first code");
mBook.setAuthor("lucky");
mBook.setPublishTime(2014);
Intent mIntent = new Intent(this,ObjectTranDemo2.class);
Bundle mBundle = new Bundle();
mBundle.putParcelable(PAR_KEY, mBook);
mIntent.putExtras(mBundle);

startActivity(mIntent);
}
//铵钮点击事件响应
public void onClick(View v) {
if(v == sButton){
SerializeMethod();
}else{
PacelableMethod();
}
}
}

2.ObjectTranDemo1.java

public class ObjectTranDemo1 extends Activity {  
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

TextView mTextView = new TextView(this);
Person mPerson = (Person)getIntent().getSerializableExtra(ObjectTranDemo.SER_KEY);
mTextView.setText("You name is: " + mPerson.getName() + "\n"+
"You age is: " + mPerson.getAge());

setContentView(mTextView);
}
}

3.ObjectTranDemo2.java

public class ObjectTranDemo2 extends Activity {  

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView mTextView = new TextView(this);
Book mBook = (Book)getIntent().getParcelableExtra(ObjectTranDemo.PAR_KEY);
mTextView.setText("Book name is: " + mBook.getBookName()+"\n"+
"Author is: " + mBook.getAuthor() + "\n" +
"PublishTime is: " + mBook.getPublishTime());
setContentView(mTextView);
}
}

4.Person.xml

public class Person implements Serializable {  
private static final long serialVersionUID = -7060210544600464481L;
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}

}

5.Book.xml

public class Book implements Parcelable {  
private String bookName;
private String author;
private int publishTime;

public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public int getPublishTime() {
return publishTime;
}
public void setPublishTime(int publishTime) {
this.publishTime = publishTime;
}

public static final Parcelable.Creator<Book> CREATOR = new Creator<Book>() {
public Book createFromParcel(Parcel source) {
Book mBook = new Book();
mBook.bookName = source.readString();
mBook.author = source.readString();
mBook.publishTime = source.readInt();
return mBook;
}
public Book[] newArray(int size) {
return new Book[size];
}
};

public int describeContents() {
return 0;
}
public void writeToParcel(Parcel parcel, int flags) {
parcel.writeString(bookName);
parcel.writeString(author);
parcel.writeInt(publishTime);
}
}

6.布局文件main.xml

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Bundle传值的两种方式"
android:layout_marginTop="40dip"
android:gravity="center"
android:textSize="22dip"
/>
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Serializable"
/>
<Button
android:id="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Parcelable"
/>
</LinearLayout>

7.配置文件AndroidManifest.xml

注册活动组件