Activity傳遞對象的方法

來源:互聯網
上載者:User

標籤:

Android中通過Intent傳遞物件類型的方法有兩種,一種是Bundle.putSerializable(Key,Object),另一種是Bundle.putParcelable(Key,Object).傳遞這些對象要滿足一定的條件,前者是實現Serializable介面,後一種是實現了Parcelable.

本文的例子是Parcelable介面的方法。

Parcelable需要實現三個函數:writeToParcel、describeContents和CREATOR.

wroteToParcel(Parcel dest,int flags)將需要數列化儲存的資料寫入外部提供的Parcel對象dest.

describeContent()描述實值型別,直接返回0即可

static final Parcelable.Creator 對象 CREATOR:這個CREATOR命名是固定的,而它對應的介面有兩個方法必須實現,createFromPrcel(Parcel source)實現從source建立出JavaBean執行個體的功能;newArray(int size)建立該類長度的數組。

實現Parcelable介面,代碼如下:

package com.activity.transfer;import android.os.Parcel;import android.os.Parcelable;public class ParcelableTest implements Parcelable{ private String string1; private String string2; @Override public int describeContents() {  // TODO Auto-generated method stub  return 0; } @Override public void writeToParcel(Parcel arg0, int arg1) {  // TODO Auto-generated method stub  arg0.writeString(string1);  arg0.writeString(string2); }  public void setString1Value(String str1) {  this.string1 = str1; } public String getString1Value() {    return this.string1; }  public void setString2Value(String str2) {  this.string2 = str2; } public String getString2Value() {    return this.string2; } public static final Parcelable.Creator<ParcelableTest> CREATOR = new Creator<ParcelableTest>() {  @Override  public ParcelableTest createFromParcel(Parcel source) {   // TODO Auto-generated method stub   ParcelableTest p = new ParcelableTest();   p.string1 = source.readString();   p.string2 = source.readString();   return p;  }  @Override  public ParcelableTest[] newArray(int size) {   // TODO Auto-generated method stub   return new ParcelableTest[size];  }     }; }

在mainActivity中建立一個按鈕,點擊按鈕後,將構造對象傳遞到activity2中,在activity中取值並顯示出來。

MainActivity.java代碼如下:

 package com.activity.transfer;import android.os.Bundle;import android.app.Activity;import android.content.Intent;import android.view.Menu;import android.view.View;import android.widget.Button;import android.widget.EditText;public class MainActivity extends Activity { Button button1 = null; Button button2 = null; EditText edit = null; @Override protected void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(R.layout.activity_main);  button1 = (Button)findViewById(R.id.button1);  button2 = (Button)findViewById(R.id.button2);  edit = (EditText)findViewById(R.id.editText1);  button1.setOnClickListener(new View.OnClickListener() {      @Override   public void onClick(View v) {    // TODO Auto-generated method stub    Intent intent = new Intent(MainActivity.this,activity2.class);    String name = edit.getText().toString();    intent.putExtra("editText", name);        //建立對象    ParcelableTest mTest = new ParcelableTest();    mTest.setString1Value(name);    mTest.setString2Value("string2");    //綁定資料到bundle    Bundle mBundle = new Bundle();    mBundle.putParcelable("DATA", mTest);    intent.putExtras(mBundle);    startActivity(intent);   }  });  button2.setOnClickListener(new View.OnClickListener() {      @Override   public void onClick(View v) {    // TODO Auto-generated method stub       }  });   } @Override public boolean onCreateOptionsMenu(Menu menu) {  // Inflate the menu; this adds items to the action bar if it is present.  getMenuInflater().inflate(R.menu.main, menu);  return true; }}

在activity2.java中顯示出來,代碼如下:

 package com.activity.transfer;import android.os.Bundle;import android.widget.TextView;import android.app.Activity;import android.content.Intent;public class activity2 extends Activity { @Override protected void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);    setContentView(R.layout.activity2);  Intent intent = this.getIntent();  String name = intent.getStringExtra("editText");  TextView text = (TextView)findViewById(R.id.textView2);  //text.setText(name);  ParcelableTest mTest = (ParcelableTest)getIntent().getParcelableExtra("DATA");  text.setText(name+"ParcelableTest傳過來的值為:"+"string1是:"+mTest.getString1Value()+"string2是:"+mTest.getString2Value()); } }

整個工程地址:

http://www.eoeandroid.com/thread-538356-1-1.html

 

Activity傳遞對象的方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.