android Parcelable



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());
		
	}
	
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值