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
时间: 2024-11-11 05:18:59