通過 Intent 傳遞類對象

來源:互聯網
上載者:User

通過 Intent 傳遞類對象
Android中Intent傳遞類對象提供了兩種方式一種是 通過實現Serializable介面傳遞對象,一種是通過實現Parcelable介面傳遞對象。

要求被傳遞的對象必須實現上述2種介面中的一種才能通過Intent直接傳遞

Intent中傳遞這2種對象的方法:

Bundle.putSerializable(Key,Object);  //實現Serializable介面的對象Bundle.putParcelable(Key, Object); //實現Parcelable介面的對象
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;}    }
註冊表單發送Intent的代碼:
    @Override    protected 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//獲得的三個組件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())?"男":"女";        //封裝成一個對象        Person p=new Person(name.getText().toString(),password.getText().toString(),sex);//建立Bundle對象Bundle bundle=new Bundle();bundle.putSerializable("person", p);//Bundle中放一個對象//建立一個Intent對象Intent intent=new Intent(MainActivity.this,ResultActivity.class);intent.putExtras(bundle);//啟動intent對應的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對象Intent intent=getIntent();//從Intent對象中擷取序列資料//Person p=(Person) intent.getSerializableExtra("person");相當於Bundle bundle=intent.getExtras();//擷取Bundle對象Person p=(Person) bundle.getSerializable("person");//Bundle對象中擷取可序列化對象name.setText(p.getName());password.setText(p.getPassword());sex.setText(p.getSex());}
以上就可以實現對象的傳遞。
如果傳遞的是List,可以把list強轉成Serializable類型,而且object類型也必須實現了Serializable介面
Intent.putExtras(key, (Serializable)list) 
(List)getIntent().getSerializable(key)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.