MainActivity:
public class MainActivity extends FragmentActivity implements OnClickListener {
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ParasBean pb = new ParasBean("test1", "test2", 3);
Intent in = new Intent(MainActivity.this, TextTwoAct.class);
in.putExtra("pb", pb);
MainActivity.this.startActivity(in);
}
}
ParasBean:
public class ParasBean implements Parcelable{
private String arg1;
private String arg2;
private int arg3;
public ParasBean(String arg1,String arg2,int arg3){
this.arg1 = arg1;
this.arg2 = arg2;
this.arg3 = arg3;
}
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
// TODO Auto-generated method stub
//写入的顺序、既是读取的顺序
dest.writeString(arg1);
dest.writeString(arg2);
dest.writeInt(arg3);
}
private ParasBean(Parcel source){
arg1 = source.readString();
arg2 = source.readString();
arg3 = source.readInt();
}
public static final Parcelable.Creator<ParasBean> CREATOR = new Creator<ParasBean>() {
@Override
public ParasBean createFromParcel(Parcel source) {
// TODO Auto-generated method stub
return new ParasBean(source);
}
@Override
public ParasBean[] newArray(int size) {
// TODO Auto-generated method stub
return new ParasBean[size];
}
};
public String getArg1() {
return arg1;
}
public void setArg1(String arg1) {
this.arg1 = arg1;
}
public String getArg2() {
return arg2;
}
public void setArg2(String arg2) {
this.arg2 = arg2;
}
public int getArg3() {
return arg3;
}
public void setArg3(int arg3) {
this.arg3 = arg3;
}
}
ShowActivity:
public class ShowActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button) this.findViewById(R.id.button1);
ParasBean pb = getIntent().getParcelableExtra("pb");
btn.setText(pb.getArg1() + "\n" + pb.getArg2() + "\n" + pb.getArg3());
}
}