反纠结app开发: activity之间的数据传递


需求:  在 第一个activity:model1 中 的两个EditText 中写入字符串, 然后传给第二个activity:  choosefrom2 供其进行随机选择.


activity 之间的数据传递是一种简单又要经常使用的实现. 以需求为例, 在 model1 中 以按钮事件触发, 取得Edittext 中的内容, 然后通过Bundle的封装传给

choosefrom2.


mybutton.setOnClickListener(new Button.OnClickListener()
		{
			@Override
			public void onClick(View arg)
			{  
				text1=myedittext1.getText().toString();
				text2=myedittext2.getText().toString();
				Intent intent =new Intent();
				intent.setClass(model1.this, choosefrom2.class);
				
				Bundle bundle = new Bundle();
				bundle.putString("text1", text1);
				bundle.putString("text2", text2);
				intent.putExtras(bundle);
				startActivity(intent);
				
			}
		});

在 choosefrom2 中,同样通过Bundle 对象来接收数据:


截取关键代码:

protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.choosefrom2);
		 
		Bundle bunde=this.getIntent().getExtras();
		text1 = bunde.getString("text1");
		text2 = bunde.getString("text2");
		

 注意提取数据时类型要与传入数据时类型一样. Bundle对象针对不同的数据类型提供了许多的方法.


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值