intent主要用在Activity与Activity之间的数据传递。除了我们平时比较经常用得简单数据传递,还可以将整一个类传递给另外一个Activity,只要这个将这个类序列化即可。这里自己敲了一个demo跟大家分享一下,希望对新手有所帮助。高手可以无视本帖。 MainActivity public class MainActivity extends Activity implements OnClickListener { private final int SERIALIZABLE = 1; private final int PARCELABLE = 2; private Button SerializableBtn; private Button ParcelableBtn; private Person p; private Person2 p2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { SerializableBtn = (Button) findViewById(R.id.serializable); ParcelableBtn = (Button) findViewById(R.id.parcelable); SerializableBtn.setOnClickListener(this); ParcelableBtn.setOnClickListener(this); } private void initSerializableDate() { p = new Person(); p.setName("Changel"); p.setPassword("123456789"); } private void initParcelablebleDate() { p2 = new Person2(); p2.setName("Changel"); p2.setPassword("123456789"); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.serializable: initSerializableDate(); Intent i = new Intent(); Bundle bundle = new Bundle(); bundle.putSerializable("person", p); i.putExtras(bundle); i.setAction("com.example.objecttrandemo.intent.action.serializable"); startActivityForResult(i, SERIALIZABLE); break; case R.id.parcelable: initParcelablebleDate(); Intent i2 = new Intent(); Bundle bundle2 = new Bundle(); bundle2.putParcelable("person", p2); i2.putExtras(bundle2); i2.setAction("com.example.objecttrandemo.intent.action.parcelable"); startActivityForResult(i2, PARCELABLE); break; } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); switch (requestCode) { case SERIALIZABLE: Toast.makeText(MainActivity.this, "实现serializable接口传递成功", Toast.LENGTH_SHORT).show(); break; case PARCELABLE: Toast.makeText(MainActivity.this, "实现parcelable接口传递成功", Toast.LENGTH_SHORT).show(); break; } } @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; } } 复制代码 ParcelableActivity public class ParcelableActivity extends Activity{ private TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test_activity); tv = (TextView) findViewById(R.id.textView1); Person2 p2 = (Person2) getIntent().getParcelableExtra("person"); tv.setText("姓名:"+p2.getName()+"\n密码:"+p2.getPassword()); setResult(Activity.RESULT_OK); } } 复制代码 SerializableActivity public class SerializableActivity extends Activity { private TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test_activity); tv = (TextView) findViewById(R.id.textView1); Person p = (Person) getIntent().getSerializableExtra("person"); tv.setText("姓名:"+p.getName()+"\n密码"+p.getPassword()); setResult(Activity.RESULT_OK); } <font size="2">}</font> 复制代码 Person public class Person implements Serializable{ String name; String password; public String getName() { return name; } public String getPassword() { return password; } public void setName(String name) { this.name = name; } public void setPassword(String password) { this.password = password; } <font size="5"><font size="2">}</font> </font> 复制代码 Person2 public class Person2 implements Parcelable { String name; String password; public String getName() { return name; } public String getPassword() { return password; } public void setName(String name) { this.name = name; } public void setPassword(String password) { this.password = password; } public static final Parcelable.Creator<Person2> CREATOR = new Creator<Person2>() { public Person2 createFromParcel(Parcel source) { Person2 person2 = new Person2(); person2.setName(source.readString()); person2.setPassword(source.readString()); return person2; } @Override public Person2[] newArray(int size) { return new Person2[size]; } }; @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(name); dest.writeString(password); } } 复制代码