Intent传值

本文介绍了在Android应用程序中使用Intent、Bundle以及自定义User类进行数据传递的方法。通过示例展示了如何从一个活动(Activity)向另一个活动发送数据,并在接收端如何获取这些数据。

一、intent传值: 分为三个方法:

一:intent.putExtra传值,

二:bundle传值  分两种 putExtra 和putExtras

三:User类传值,

发送的代码:

protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		bt = (Button) findViewById(R.id.bt);
		bt.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent i = new Intent(MainActivity.this, SecondActivity.class);
				//i.putExtra("key", "嘟嘟嘟嘟的");
				//i.putExtra("key1", "啦啦啦啦啦了");
				//``Bundle bundle=new Bundle();
				//bundle.putString("a", "y");
				//bundle.putString("c", "z");
				//``i.putExtras(bundle);
				//```i.putExtra("abc", bundle);
				User user=new User("啊啊啊", 18);
				i.putExtra("key", user);
				
				startActivity(i);
			}
		});
接受的代码:

protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_second);
		tv = (TextView) findViewById(R.id.tv);
		Intent intent = getIntent();
		//Bundle b=intent.getExtras();
		//``Bundle b=intent.getBundleExtra("abc");
		//``String s=b.getString("a");
		//``String s1=b.getString("c");
		//String s = intent.getStringExtra("key");
		//String s1=intent.getStringExtra("key1");
		User user=(User) intent.getSerializableExtra("key");
		tv.setText(user.getName()+user.getAge());
	}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值