你如何从一个活动传递一个字符串到另一个? [英] How do you pass a string from one activity to another?
问题描述
我不知道如何传递和读取一个字符串,它是从另一个活动的一项活动。我有两个活动。我会打电话给他们的活动1和活性2。我有一个字符串在活动1名为当然
。我想读的字符串活性2。
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.
公共类活性2扩展活动1 {
我见过的人使用意图的功能,但我无法弄清楚如何使用它。
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屋!