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();
本文详细介绍了在Android应用中如何使用Intent在不同Activity间传递数据,包括字符串、整数及自定义对象的传递方法。首先展示了如何通过putExtra方法添加字符串数据,并在目标Activity中通过getStringExtra方法获取。接着说明了通过Bundle对象封装整数数据进行传递的过程,最后演示了如何序列化自定义对象并通过Intent进行传递。
3173

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



