Android之Activity之間傳遞對象
阿新 • • 發佈:2017-07-06
this .get tin 方式 art desc 內容 ext pri
當我們點擊了FirstActivity的界面上的"開始傳遞對象"button後,跳轉到SecondActivity,界面例如以下:
在非常多時候,我們須要在Activity之間傳遞對象,比方當你點擊了某列表的item,須要傳遞給下一個Activity該對象,那我們須要該怎麽做呢?
Android支持兩種傳遞對象的方式。一種是bundle.putSerializable方式,一種是bundle.putParcelable。
那麽以下我們就用一個樣例來實踐Activity傳遞對象:
1.首先建立兩個類,一個Teacher類表示老師,一個Student類表示學生。內容分別例如以下:
<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的布局:
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; } } }
裏面就僅僅有一個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,界面例如以下:
非常顯然,這兩種方式都能夠成功的在Activity之間傳遞對象。
希望以上內容對各位讀者可以帶來幫助!
Android之Activity之間傳遞對象