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

San*_*V M 27 android

我用AutuCompleteTextView [ACTV]和按钮创建了一个Activity.我在ACTV输入一些文字,然后按下按钮.按下按钮后,我希望活动转到另一个活动.在第二个Activity中我只想将在ACTV(第一次动作中)输入的文本显示为TextView.

我知道如何开始第二项活动,如下所示:

Intent i = new Intent(this, ActivityTwo.class);
startActivity(i);
Run Code Online (Sandbox Code Playgroud)

我编写了这个来获取从ACTV输入的文本.

AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete);
CharSequence getrec=textView.getText();
Run Code Online (Sandbox Code Playgroud)

我的问题是如何将"getrec"(在我按下按钮后)从第一个Activity传递到第二个Activity.后来在第二次活动中收到"getrec".

请假设我已使用"onClick(View v)"为按钮创建了事件处理程序类

DeR*_*gan 60

您可以使用Bundle在Android中执行相同的操作

创建意图:

Intent i = new Intent(this, ActivityTwo.class);
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete);
String getrec=textView.getText().toString();

//Create the bundle
Bundle bundle = new Bundle();

//Add your data to bundle
bundle.putString(“stuff”, getrec);

//Add the bundle to the intent
i.putExtras(bundle);

//Fire that second activity
startActivity(i);
Run Code Online (Sandbox Code Playgroud)

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

//Get the bundle
Bundle bundle = getIntent().getExtras();

//Extract the data…
String stuff = bundle.getString(“stuff”); 
Run Code Online (Sandbox Code Playgroud)


use*_*RKS 6

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

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

//Create the `intent`
 Intent i = new Intent(this, ActivityTwo.class);
String one="xxxxxxxxxxxxxxx";
String two="xxxxxxxxxxxxxxxxxxxxx";
//Create the bundle
Bundle bundle = new Bundle();
//Add your data to bundle
bundle.putString(“ONE”, one);
bundle.putString(“TWO”, two);  
//Add the bundle to the intent
i.putExtras(bundle);
//Fire that second activity
startActivity(i);
Run Code Online (Sandbox Code Playgroud)

否则你可以putExtra()直接使用意图发送数据和getExtra()获取数据.

Intent i=new Intent(this, ActivityTwo.class);
i.putExtra("One",one);
i.putExtra("Two",two);
startActivity(i);
Run Code Online (Sandbox Code Playgroud)