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

30

我创建了一个带有AutuCompleteTextView[ACTV]和按钮的Activity。我在ACTV中输入一些文本,然后按下按钮。

按下按钮后,我希望Activity跳转到另一个Activity。在第二个Activity中,我只想将第一个Activity中输入的文本显示为TextView。

我知道如何启动第二个Activity,代码如下:

Intent i = new Intent(this, ActivityTwo.class);
startActivity(i);

我编写了这段代码来获取从ACTV输入的文本。

AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete);
CharSequence getrec=textView.getText();

我的问题是如何在第一个Activity中按下按钮后传递“getrec”,并在第二个Activity中接收“getrec”。

请假设我已经使用“onClick(View v)”创建了按钮的事件处理程序类。

7个回答

73

您可以在Android中使用Bundle来实现相同的功能

创建Intent:

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);

现在在你的第二个活动中,从 bundle 中检索数据:

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

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

你不需要创建一个bundle实例。相反,你可以使用"i.putExtra("stuff", getrec)"来完成。 - ladytoky0

7

从一个Activity传递数据到另一个Activity的标准方法:

如果您想要从一个Activity向另一个Activity传递大量数据,则可以将数据放入Bundle中,然后使用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);

否则,您可以直接使用意图(Intent)和 putExtra() 发送数据,并使用 getExtra() 获取数据。
Intent i=new Intent(this, ActivityTwo.class);
i.putExtra("One",one);
i.putExtra("Two",two);
startActivity(i);

1

如果您要从A传递字符串X到B,那么很简单。
A--> B

在Activity A中
1)创建Intent
2)使用Intent的putExtra方法将数据放入Intent中
3)启动Activity

Intent i = new Intent(A.this, B.class);
i.putExtra("MY_kEY",X);

在B活动中
在onCreate方法内
1)获取意图对象
2)使用键(MY_KEY)获取存储的值

Intent intent = getIntent();
String result = intent.getStringExtra("MY_KEY");

这是从A到B发送数据的标准方式。 您可以发送任何数据类型,例如int、boolean、ArrayList和String[]。根据在Activity中存储的数据类型作为键值对,检索方法可能会有所不同,例如如果您传递int值,则将调用
intent.getIntExtra("KEY");

你甚至可以发送类对象,但是为此,你必须使你的类对象实现Serializable或Parceable接口。
TransactionTooLargeException
你可以发送多少数据。如果数据大小超过一定限制,则可能会出现TransactionTooLargeException异常。例如,如果你试图在活动之间发送位图,并且大小超过了某个数据大小,则可能会看到此异常。

1

按照这种方式实现

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

开始第二个activity,在这个class里使用Bundle值,请使用以下代码

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

1

1
以下是示例Kotlin代码:

页面1


val i = Intent(this, Page2::class.java)
            val getrec = list[position].promotion_id
            val bundle = Bundle()
            bundle.putString("stuff", getrec)
            i.putExtras(bundle)
            startActivity(i)

第二页
        var bundle = getIntent().getExtras()
        var stuff = bundle.getString("stuff")

0

在第一个活动中:

Intent i=new Intent(getApplicationContext,secondActivity.class);

i.putExtra("key",value);

startActivity(i);

在SecondActivity中:

String value=getIntent.getStringExtra("Key");

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接