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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:13:34  来源:igfitidea点击:

How to pass value of one TextView to another TextView in different Activity

android

提问by AndroidDev

Possible Duplicate:
How to pass object from one activity to another in Android

可能的重复:
如何在 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 Buttonand in 2.xml i have TextView. So what i want is on click of Buttonwhich is on first activity i want to open the 2nd activity and also want to display the text show in Buttonon the Textviewpresent in Activity2. I mean to say that suppose the text in the Buttonis "ADD" then this text will be displayed in TextView.

实际上我有两个活动活动 1 和活动 2。对于这两个活动,我有两个 xml 布局 1.xml 和 2.xml。在 1.xml 我有一个Button和在 2.xml 我有TextView。所以,我想是在点击Button这是第一次活动,我想开第二个活动,也希望在显示文本显示ButtonTextview存在的活性2。我的意思是说,假设 中的文本Button是“ADD”,那么该文本将显示在TextView.

Note: Buttonis on Activity1 and TextViewis on Activity2

注意:Button在 Activity1 和TextViewActivity2 上

回答by Mohammed Azharuddin Shaikh

INTENTUse Intentto 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.

您需要根据您的意图添加额外的参数。观看这篇文章