Android的Activity界面跳转之间的数据传递

使用Intent传递数据的有两种方式。

1.通过Intent的putExtra()方法传递数据

putExtra()方法包含两个参数,第一个参数表示传递的数据名称,第二个参数表示传递的数据信息。

例子:

Intent i=new Intent();
i.setClass(MainActivity.this,SecondActivity.class);//设置跳转到的Activity
i.putExtra("test","123");//传递的值
i.putExtra("test2","456");
startActivity(i);

通过putExtra()方法将传递的数据存储在Intent对象后,通关getXXXExtra()方法来获取数据。

Intent i=getIntent();
String name=i.getStringExtra("test");
String name2=i.getStringExtra("test2");

2.使用Bundle类传递数据

Bundle类与Map接口比较类似,都是通过键值对的形式来保存数据。

Intent i=new Intent();
i.setClass(this,SecondActivity.class);
Bundle bundle=new Bundle();//创建Bundle对象
bundle.putString("test","123");
bundle.putString("test2","456");
i.putExtras(bundle);
startActivity(i);

获取数据

Bundle bundle=getIntent().getExtras();
String test=bundle.getString("test");
String test2=bundle.getString(test2"):

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值