Android之Activity之间传递对象

犀渠玉剑良家子,白马金羁侠少年。这篇文章主要讲述Android之Activity之间传递对象相关的知识,希望能为你提供帮助。
在非常多时候,我们须要在Activity之间传递对象,比方当你点击了某列表的item,须要传递给下一个Activity该对象,那我们须要该怎么做呢?


android支持两种传递对象的方式。一种是bundle.putSerializable方式,一种是bundle.putParcelable。


那么以下我们就用一个样例来实践Activity传递对象:


1.首先建立两个类,一个Teacher类表示老师,一个Student类表示学生。内容分别例如以下:
【Android之Activity之间传递对象】

< span style="font-size:18px; "> public class Teacher implements Serializable { private static final long serialVersionUID = -7060210544600464481L; private String name; private int age; private String addr; public Teacher(String name, int age, String addr) { super(); this.name = name; this.age = age; this.addr = addr; } 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; } public String getAddr() { return addr; } public void setAddr(String addr) { this.addr = addr; } @Override public String toString() { String temp = "老师姓名:" + this.name + " 年龄:" + this.age + " 地址:" + this.addr; return temp; }}< /span>




< span style="font-size:18px; "> import android.os.Parcel; import android.os.Parcelable; public class Student implements Parcelable { private String name; private int age; private String addr; public static final Parcelable.Creator< Student> CREATOR = new Creator< Student> () {@Override public Student[] newArray(int size) { return new Student[size]; }@Override public Student createFromParcel(Parcel source) { Student stu = new Student(); stu.name = source.readString(); stu.age = source.readInt(); stu.addr = source.readString(); return stu; } }; @Override public int describeContents() { // TODO Auto-generated method stub return 0; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(name); dest.writeInt(age); dest.writeString(addr); } public Student(){ } public Student(String name, int age, String addr) { super(); this.name = name; this.age = age; this.addr = addr; } 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; } public String getAddr() { return addr; } public void setAddr(String addr) { this.addr = addr; } @Override public String toString() { String temp = "学生姓名:" + this.name + " 年龄:" + this.age + " 地址:" + this.addr; return temp; } }< /span>



当中Teacher类实现了Serializable接口,Student类实现了Parcelable接口。


2.创建两个Activity,第一个Activity叫做FirstActivity。它里面有两个对象:一个Teacher对象,一个Student对象,我们如今须要将这两个对象传递给第二个Activity。即SecondActivity。



首先我们看一下FirstActivity的布局:
Android之Activity之间传递对象

文章图片




FirstActivity的内容例如以下:

import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; public class FirstActivity extends Activity implements OnClickListener{ public static final String TEACHER_KEY = "key_teacher"; public static final String STUDENT_KEY = "key_student"; private Teacher teacher = new Teacher("刘备", 36, "荆州"); private Student student = new Student("张飞", 32, "新野"); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_first); findViewById(R.id.button1).setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.button1: Bundle bundle = new Bundle(); bundle.putSerializable(TEACHER_KEY, teacher); bundle.putParcelable(STUDENT_KEY, student); Intent intent = new Intent(FirstActivity.this,SecondActivity.class); intent.putExtras(bundle); startActivity(intent); break; default: break; } }}






然后我们再看一下SecondActivity的布局:
Android之Activity之间传递对象

文章图片

里面就仅仅有一个TextView。



然后我们看一下SecondActivity的内容,它主要是将传递过来的对象显示到TextView其中:

import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.widget.TextView; public class SecondActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); Intent intent = this.getIntent(); Bundle bundle = intent.getExtras(); Teacher t = (Teacher) bundle.getSerializable(FirstActivity.TEACHER_KEY); Student s = bundle.getParcelable(FirstActivity.STUDENT_KEY); String text = t.toString() + "\n" + s.toString(); ((TextView)findViewById(R.id.text_view)).setText(text); }}


当我们点击了FirstActivity的界面上的"開始传递对象"button后,跳转到SecondActivity,界面例如以下:
Android之Activity之间传递对象

文章图片




非常显然,这两种方式都能够成功的在Activity之间传递对象。


希望以上内容对各位读者可以带来帮助!



















    推荐阅读