java 如何将字符串从一个活动传递到另一个活动?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15859445/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you pass a string from one activity to another?
提问by 79t97g
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.
我想知道如何从另一个活动传递和读取一个活动中的字符串。我有两个活动。我将它们称为 Activity1 和 Activity2。我在 Activity1 中有一个名为course
. 我想在 Activity2 中读取该字符串。
I've tried doing this but the string came out empty.
我试过这样做,但字符串出来是空的。
public class Activity2 extends Activity1 {
public class Activity2 extends Activity1 {
I've seen people use the Intent function but I couldn't figure out how to use it.
我见过人们使用 Intent 函数,但我不知道如何使用它。
Any suggestions? Thanks!
有什么建议?谢谢!
回答by Raghunandan
Pass values using intents.
使用意图传递值。
In your first activity
在你的第一个活动中
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/
http://www.technotalkative.com/android-send-object-from-one-activity-to-another-activity/
回答by Amol Sawant 96 Kuli
your first activity, Activity1
你的第一个活动, Activity1
public class Activity1 extends Activity {
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity1);
btn=(Button) findViewById(R.id.payBtn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent=new Intent(Activity1.this,Activity2.class);
intent.putExtra("course", "courseValue");
startActivity(intent);
}
});
}
}
Activity2
public class Activity2 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity2);
String course=getIntent().getExtras().getString("course").toString();
Log.d("course",course);
}
}
Hope this will help you.
希望这会帮助你。
回答by lokoko
From Activity 1 call something like this :
从活动 1 调用如下:
Intent intent= new Intent("path.secondActivity");
intent.putExtra("keyString",sampleString);
startActiivty(intent);
and in activity 2 try something like this :
在活动 2 中尝试这样的事情:
Bundle values = getIntent().getExtras();
if (values != null) {
String keyString = values.getString("keyString");
}
回答by lokoko
In your MainActivity
在你的主活动中
Intent i= new Intent(MainActivity.this,SecondActivity.class);
i.putExtra("key",yourstring);
startActiivty(i);
In your second activity onCreate()
在你的第二个活动 onCreate()
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("key");
}
回答by drew moore
You're on the right track - you're using an intent to launch the second activity. All you have to do is add intent.putExtra("title", stringObject);
where stringObject is the string you want to pass, and title is the name you want to give that object. You use that name to refer the object passed in the second activity as follows:
您走在正确的轨道上 - 您正在使用意图启动第二个活动。您所要做的就是添加intent.putExtra("title", stringObject);
其中 stringObject 是您要传递的字符串,而 title 是您要为该对象提供的名称。您可以使用该名称来引用在第二个活动中传递的对象,如下所示:
String s = (String)getIntent().getExtras().getSerializable("title");
回答by Mohan Raj B
Try this
试试这个
public class Activity2 extends Activity1
公共类 Activity2 扩展了 Activity1