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();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

庭博

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值