在android的两项活动之间如何可以传输数据 [英] How can I transfer the data between two activities in android

查看:109
本文介绍了在android的两项活动之间如何可以传输数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
  <一href="http://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android">How我传递活动之间的数据在Android中?

我有两个活动A和B.活动A有1个EDITTEXT和button.Activity B的EDITTEXT,当我输入的东西在A的EDITTEXT,然后单击按钮,它应该得到显示在editText2。任何人都可以提供code这一点。

I have two activities A and B. Activity A has one editText and a button.Activity B has a editText, when I type something in editText of A and click the button it should get displayed in editText2. Can anyone provide code for this.

在此先感谢!

推荐答案

我会假设你已经写了两本活动类: ActivityA &放大器; ActivityB 键,你已经写了 onClickListener 在按钮 ActivityA

I will assume that you have written two Activity classes: ActivityA & ActivityB and that you have written the onClickListener for the button in ActivityA.

要在两个活动之间传递数据,您需要使用Intent类,通过它你开始的活动,只是startActivity为ActivityB之前,您可以通过额外的对象填充数据。对你来说,这将是EDITTEXT的内容。

To pass data between two activities, you will need to use the Intent class via which you are starting the Activity and just before startActivity for ActivityB, you can populate it with data via the Extra objects. In your case, it will be the content of the editText.

Intent i = new Intent(getBaseContext(),ActivityB.class);

//Set the Data to pass
EditText txtInput = (EditText)findViewById(R.id.txtInput);
String txtData = txtInput.getText().toString();
i.putExtra("txtData", txtData);

startActivity(i);

现在在ActivityB,你可以写code在OnCreate得到启动它的意图,并提取出来传递给它的数据。

Now in ActivityB, you can write code in the onCreate to get the Intent that launched it and extract out the data passed to it.

Intent i = getIntent();
//The second parameter below is the default string returned if the value is not there. 
String txtData = i.getExtras().getString("txtData","");
EditText txtInput2 = (EditText)findViewById(R.id.txtInput2);
txtInput2.setText(txtData);

希望这有助于。

Hope this helps.

这篇关于在android的两项活动之间如何可以传输数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆