Bundle 传参
传送端
1.首先找到事件触发的id
mBundleButton = (Button) findViewById(R.id.intent_bundle_button);
2.实例化Bundle
Bundle bundle = new Bundle();
3.传入想要传入的值
bundle.putString("NAME",name);
bundle.putString("BIRTH",birth);
bundle.putString("SEX",sex);//putXX有多种方法可选,是具体参数而定
4.实例化Intent,放入Bundle实例
Intent intent2=new Intent(this,Show_Activity.class);
intent2.putExtra("BUNDLE",bundle);//键值对,BUNDLE,是找寻bundle的标志
5.启动Activity
接收端
1.信息显示的id
showaddress= (EditText)findViewById(R.id.intent_show_address);
2.得到intent
Intent intent =getIntent();//不是new,是得到已有的用 getIntent();
3.实现的方法
bundleTypeIntent(intent);//intent作为联系四大组件的枢纽,有它才能有数据
4. public void bundleTypeIntent(Intent intent){
Bundle bundle = intent.getBundleExtra("BUNDLE");//是Bundle独有的getBundleExtra()方法
String name = bundle.getString("NAME");
}
传送端
1.首先找到事件触发的id
mBundleButton = (Button) findViewById(R.id.intent_bundle_button);
2.实例化Bundle
Bundle bundle = new Bundle();
3.传入想要传入的值
bundle.putString("NAME",name);
bundle.putString("BIRTH",birth);
bundle.putString("SEX",sex);//putXX有多种方法可选,是具体参数而定
4.实例化Intent,放入Bundle实例
Intent intent2=new Intent(this,Show_Activity.class);
intent2.putExtra("BUNDLE",bundle);//键值对,BUNDLE,是找寻bundle的标志
5.启动Activity
接收端
1.信息显示的id
showaddress= (EditText)findViewById(R.id.intent_show_address);
2.得到intent
Intent intent =getIntent();//不是new,是得到已有的用 getIntent();
3.实现的方法
bundleTypeIntent(intent);//intent作为联系四大组件的枢纽,有它才能有数据
4. public void bundleTypeIntent(Intent intent){
Bundle bundle = intent.getBundleExtra("BUNDLE");//是Bundle独有的getBundleExtra()方法
String name = bundle.getString("NAME");
showaddress.setText(name);//信息显示实例
}
本文介绍了Android中如何使用Bundle进行数据传递,通过实例展示了如何将参数放入Bundle并传递到目标Activity,然后在接收端如何取出并显示数据。
618

被折叠的 条评论
为什么被折叠?



