猿问

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

如何在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)”为按钮创建了事件处理程序类。


函数式编程
浏览 773回答 3
3回答

达令说

您可以在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”);

万千封印

将数据从一项活动传递到另一项活动的标准方法:如果要将大量数据从一个活动发送到另一个活动,则可以将数据放入一个包中,然后使用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猫小萌

以这种方式实施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");
随时随地看视频慕课网APP

相关分类

Android
我要回答