如何将字符串从一个活动传递到另一个活动? [英] How do you pass a string from one activity to another?
问题描述
我想知道如何从另一个活动传递和读取一个活动中的字符串.我有两个活动.我将它们称为 Activity1 和 Activity2.我在 Activity1 中有一个名为 course
的字符串.我想在 Activity2 中读取该字符串.
I'm wondering how to pass and read a string that is in one activity from another activity. I have two activities. I'll call them Activity1 and Activity2. I have a string in Activity1 called course
. I want to read that string in Activity2.
我试过这样做,但字符串是空的.
I've tried doing this but the string came out empty.
public class Activity2 extends Activity1 {
我见过有人使用 Intent 函数,但我不知道如何使用它.
I've seen people use the Intent function but I couldn't figure out how to use it.
有什么建议吗?谢谢!
推荐答案
使用意图传递值.
在您的第一个活动中
Intent i= new Intent("com.example.secondActivity");
i.putExtra("key",mystring);
// for explicit intents
// Intent i= new Intent(ActivityName.this,SecondActivity.class);
// parameter 1 is the key
// parameter 2 is the value
// your value
startActivity(i);
在您的第二个活动中检索它.
In your second activity retrieve it.
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("key");
//get the value based on the key
}
要传递自定义对象,您可以查看此链接
To pass custom objects you can have a look at this link
http://www.technotalkative.com/android-send-object-from-one-activity-to-another-activity/
这篇关于如何将字符串从一个活动传递到另一个活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!