为了账号安全,请及时绑定邮箱和手机立即绑定

如何在Android中将一个值从一个活动传递到另一个活动?

如何在Android中将一个值从一个活动传递到另一个活动?

函数式编程 2019-07-20 13:20:40
如何在Android中将一个值从一个活动传递到另一个活动?我用AutuCompleteTextView[ACTV]和按钮创建了一个活动。我在ACTV中输入一些文本,然后按下按钮。按下按钮后,我希望活动转到另一个活动。在第二个活动中,我只想将在ACTV(第一个Actvity)中输入的文本显示为TextView。我知道如何开始第二项活动,具体如下:Intent i = new Intent(this, ActivityTwo.class);startActivity(i);我对此进行了编码,以获得从ACTV输入的文本。AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete);CharSequence getrec=textView.getText();这里的问题是如何将“getrec”(在我按下按钮后)从第一个活动传递到第二个活动。后来在第二次活动中收到了“getrec”。请假定我已经使用“onClick(Viewv)”为按钮创建了事件处理程序类。
查看完整描述

3 回答

?
达令说

TA贡献1821条经验 获得超6个赞

您可以在Android中使用Bundle进行同样的操作。

创建意图:

Intent i = new Intent(this, ActivityTwo.class);AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete);String getrec=textView.getText().toString();//Create the bundleBundle bundle = new Bundle();//Add your data to bundlebundle.putString(“stuff”, getrec);//Add the bundle to the intenti.putExtras(bundle);//Fire that second activitystartActivity(i);

现在,在第二个活动中,从包中检索数据:

//Get the bundleBundle bundle = getIntent().getExtras();//Extract the data…String stuff = bundle.getString(“stuff”);


查看完整回答
反对 回复 2019-07-20
?
万千封印

TA贡献1891条经验 获得超3个赞

将数据从一项活动传递到另一项活动的标准方法:

如果要将大量数据从一个活动发送到另一个活动,则可以将数据放入一个包中,然后使用putExtra()方法。

//Create the `intent`
 Intent i = new Intent(this, ActivityTwo.class);String one="xxxxxxxxxxxxxxx";String two="xxxxxxxxxxxxxxxxxxxxx";//Create the bundleBundle bundle = new Bundle();//Add your data to bundlebundle.putString(“ONE”, one);bundle.putString(“TWO”, two);  //Add the bundle to the intenti.putExtras(bundle);//Fire that second activitystartActivity(i);

否则您可以使用putExtra()直接发送数据和getExtra()来获取数据。

Intent i=new Intent(this, ActivityTwo.class);i.putExtra("One",one);i.putExtra("Two",two);startActivity(i);


查看完整回答
反对 回复 2019-07-20
?
Smart猫小萌

TA贡献1911条经验 获得超7个赞

以这种方式实施

String i="hi";Intent i = new Intent(this, ActivityTwo.class);//Create the bundleBundle b = new Bundle();//Add your data to bundleb.putString(“stuff”, i);i.putExtras(b);startActivity(i);

开始那第二次activity,在这里面class若要使用绑定值,请使用以下代码

Bundle bundle = getIntent().getExtras();String text= bundle.getString("stuff");


查看完整回答
反对 回复 2019-07-20
  • 3 回答
  • 0 关注
  • 772 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信