Android 如何将一个 TextView 的值传递给不同 Activity 中的另一个 TextView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10023694/
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 to pass value of one TextView to another TextView in different Activity
提问by AndroidDev
Possible Duplicate:
How to pass object from one activity to another in Android
Actually i have two activity Activity 1 and Activity 2. For these two activity i have two xml layout 1.xml and 2.xml. In 1.xml i have a Button
and in 2.xml i have TextView
. So what i want is on click of Button
which is on first activity i want to open the 2nd activity and also want to display the text show in Button
on the Textview
present in Activity2. I mean to say that suppose the text in the Button
is "ADD" then this text will be displayed in TextView
.
实际上我有两个活动活动 1 和活动 2。对于这两个活动,我有两个 xml 布局 1.xml 和 2.xml。在 1.xml 我有一个Button
和在 2.xml 我有TextView
。所以,我想是在点击Button
这是第一次活动,我想开第二个活动,也希望在显示文本显示Button
上Textview
存在的活性2。我的意思是说,假设 中的文本Button
是“ADD”,那么该文本将显示在TextView
.
Note: Button
is on Activity1 and TextView
is on Activity2
注意:Button
在 Activity1 和TextView
Activity2 上
回答by Mohammed Azharuddin Shaikh
INTENTUse Intent
to pass data, putExtra()
will allow you to put data
意向使用Intent
来传递数据,putExtra()
将让你把数据
ActivityA
活动A
Intent myIntent = new Intent(ActivityA.this, Activityb.class);
myIntent.putExtra("key", "value");
startActivity(myIntent);
ActivityB
活动B
Intent myIntent = getIntent(); // this is just for example purpose
myIntent.getExtra("key");
回答by ρяσ?ρ?я K
try this using Intent:
使用 Intent 试试这个:
In Activity 1 on Button Click:
在按钮单击的活动 1 中:
Intent intent = new Intent(Activityone.class, Activitytwo.class);
intent.putExtra("value2","world");
startActivity(intent);
In Activity 2:
在活动 2 中:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String value1 = super.getIntent().getExtras().getString("value1");
myTextView.setText("value1: " + value1 + ");
}
回答by Praveenkumar
First Activity -
第一个活动 -
Button btn = (Button)findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String passingdata = textview.getText().toString();
Intent i = new Intent(Activity1.this, Activity2.class);
Bundle b = new Bundle();
b.putString("Key", passingdata);
i.putExtras(b);
startActivity(i);
}
});
Second Activity -
第二个活动 -
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
Bundle b = getIntent().getExtras();
String receivingdata = b.getStringExtra("Key");
TextView tv = (TextView)findViewById(R.id.secondtext);
tv.setText(receivingdata);
}
回答by Sandip Jadhav
While starting new activity in your button click write below code
在您的按钮中开始新活动时,请单击下面的代码
Intent intent = new Intent();
intent.putExtra("TextValue", text1.getText().toString());
intent.setClass(Activity1.this, Activity2.class);
startActivity(intent);
In your Activity2 in onCreate()
在你的 Activity2 中 onCreate()
String s = getIntent().getStringExtra("TextValue");
回答by yves.beutler
You need to put extra parameters on your intent. Watch this article.
您需要根据您的意图添加额外的参数。观看这篇文章。