如何在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个赞
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”);
万千封印
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);
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");
- 3 回答
- 0 关注
- 772 浏览
添加回答
举报
0/150
提交
取消