Intent传递数据的实例

Intent 传送一个字符串:


A机:
Intent zgdnb=new Intent(MainActivity.this,Main2Activity.class);
zgdnb.putExtra("str_data","春眠不觉晓,\n波波不洗澡。\n夜来风雨声,\n花落知多少");
startActivity(zgdnb);
B机:
String str_data=zgdnb.getStringExtra("str_data");
final TextView textView1=(TextView)findViewById(R.id.textView1);
textView1.setText(cx);

Intent传送一个整数:


在android系统中的intent对象是不支持直接传递int数据类型的。
解决这类问题有两种方法:一是通过数据类型转换,不过在有些特殊的情况下这种方法并不适用;
二是通过bundle这个对象来封装数据进行传递,
A机: 
Bundle bundle=new Bundle();
bundle.putInt("age",20);
zgdnb.putExtras(bundle);
B机:
Bundle bundle=getIntent().getExtras();
int age=bundle.getInt("age");
String myage=age+" ";//转成字符串
final TextView textView2=(TextView)findViewById(R.id.textView2);
textView2.setText(myage);

Intent传送一个对象:


第一步:建一个Student对象
public class Student implements Serializable{}
第二步:A机
Student stu1=new Student(100,"金英兴",70.8f);
bundle.putSerializable("stu1", stu1);
zgdnb.putExtras(bundle);
startActivity(zgdnb);
第三步:B机
Student stu1=(Student) zgdnb.getSerializableExtra("stu1");
Toast.makeText(this,stu1.toString(),Toast.LENGTH_LONG).show();

发布了72 篇原创文章 · 获赞 79 · 访问量 22万+

猜你喜欢

转载自blog.csdn.net/jintingbo/article/details/104639286
今日推荐