通过 Intent 传递类对象
生活随笔
收集整理的這篇文章主要介紹了
通过 Intent 传递类对象
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Android中Intent傳遞類對(duì)象提供了兩種方式一種是 通過實(shí)現(xiàn)Serializable接口傳遞對(duì)象,一種是通過實(shí)現(xiàn)Parcelable接口傳遞對(duì)象。
如果傳遞的是List<Object>,可以把list強(qiáng)轉(zhuǎn)成Serializable類型,而且object類型也必須實(shí)現(xiàn)了Serializable接口
要求被傳遞的對(duì)象必須實(shí)現(xiàn)上述2種接口中的一種才能通過Intent直接傳遞
Intent中傳遞這2種對(duì)象的方法:
Bundle.putSerializable(Key,Object); //實(shí)現(xiàn)Serializable接口的對(duì)象 Bundle.putParcelable(Key, Object); //實(shí)現(xiàn)Parcelable接口的對(duì)象Person.java
package com.hust.bundletest;import java.io.Serializable;public class Person implements Serializable {String name;String password;String sex;public Person(String name, String password, String sex) {super();this.name = name;this.password = password;this.sex = sex;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}} 注冊(cè)窗體發(fā)送Intent的代碼:
@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button btn=(Button) findViewById(R.id.button1);btn.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v) {// TODO Auto-generated method stub//獲得的三個(gè)組件EditText name=(EditText) findViewById(R.id.name);EditText password=(EditText) findViewById(R.id.password);RadioButton male=(RadioButton) findViewById(R.id.radio0);//判斷是否被選String sex=(male.isChecked())?"男":"女";//封裝成一個(gè)對(duì)象Person p=new Person(name.getText().toString(),password.getText().toString(),sex);//創(chuàng)建Bundle對(duì)象Bundle bundle=new Bundle();bundle.putSerializable("person", p);//Bundle中放一個(gè)對(duì)象//創(chuàng)建一個(gè)Intent對(duì)象Intent intent=new Intent(MainActivity.this,ResultActivity.class);intent.putExtras(bundle);//啟動(dòng)intent對(duì)應(yīng)的ActivitystartActivity(intent); }});接收端的代碼:
@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_result);//獲取顯示組件TextView name=(TextView) findViewById(R.id.text1);TextView password=(TextView) findViewById(R.id.text2);TextView sex=(TextView) findViewById(R.id.text3);//獲取Intent對(duì)象Intent intent=getIntent();//從Intent對(duì)象中獲取序列數(shù)據(jù)//Person p=(Person) intent.getSerializableExtra("person");相當(dāng)于Bundle bundle=intent.getExtras();//獲取Bundle對(duì)象Person p=(Person) bundle.getSerializable("person");//Bundle對(duì)象中獲取可序列化對(duì)象name.setText(p.getName());password.setText(p.getPassword());sex.setText(p.getSex());}以上就可以實(shí)現(xiàn)對(duì)象的傳遞。
如果傳遞的是List<Object>,可以把list強(qiáng)轉(zhuǎn)成Serializable類型,而且object類型也必須實(shí)現(xiàn)了Serializable接口
Intent.putExtras(key, (Serializable)list) (List<YourObject>)getIntent().getSerializable(key)
總結(jié)
以上是生活随笔為你收集整理的通过 Intent 传递类对象的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Activity的生命周期理解
- 下一篇: Intent对象详解(一)