简体   繁体   English

如何将edittext值传递给另一个活动的edittext?

[英]How to pass edittext value to another activity's edittext?

我的项目的要求是:edittext 值首先由用户输入,并且该值将在另一个活动的 editext 中可见,该值应该是只读的。

You can pass it using Intent's putExtra() method.您可以使用 Intent 的 putExtra() 方法传递它。 try this way,试试这个方法

In First Activity,在第一个活动中,

Intent intent = new Intent ( FirstAcvity.this, SecondActivity.class ); 
intent.putExtra ( "TextBox", editText.getText().toString() );
startActivity(intent); 

Now, in second activity, use following code,现在,在第二个活动中,使用以下代码,

Intent i = getIntent(); 
String text = i.getStringExtra ( "TextBox","" ); 
// Now set this value to EditText 
secondEditText.setText ( text ); 
secondEditText.setEnable(false);

get the value of edit text by通过获取编辑文本的值

String text = edit_text.getText.toString;

then pass it to other activity like然后将其传递给其他活动,例如

intent.putExtra("text", text);

In that activity get it onCreate through bundle like在该活动中通过捆绑包获得它 onCreate

Bundle extras = getExtra().getIntent();
String text = extras.getString("text");

now set this value in your edittext like现在在您的编辑文本中设置此值,例如

edit_text2.setText(text);

modify this code according to you.根据您修改此代码。

You can send N number of data from one activity to other activity like below :您可以将 N 个数据从一个活动发送到另一个活动,如下所示:

Sneder : Current.java斯奈德: Current.java

Intent values = new Intent(Current.this, Destination.class);
//values.putExtra("KEY","VALUE");
values.putExtra("USER_NAME",user_name);
startActivity(values);
finish();

Receiver : Destination.java接收方: Destination.java

String value = getIntent().getStringExtra("USER_NAME");

Based on Lucifer's answer you could use a TextView in the second activity:根据 Lucifer 的回答,您可以在第二个活动中使用 TextView:

First Activity:第一个活动:

Intent intent = new Intent ( FirstAcvity.this, SecondActivity.class ); 
intent.putExtra ( "text", editText.getText().toString() );
startActivity(intent); 

Second Activity:第二个活动:

Intent i = getIntent();
tv.setText(i.getStringExtra("text"); //tv is the TextView

Basically its not a big deal to pass edittext value to textview present in other activity just follow 2steps....基本上将edittext值传递给其他活动中存在的textview并不是什么大问题,只需按照2个步骤....

PROCESS过程

  1. get two Activities(java classes)获得两个活动(java 类)

  2. in first Activity take editview在第一个活动中进行编辑视图

  3. in second Activity take textview在第二个活动中采取 textview

  4. dont forget to create a button and set Onclicklister to it不要忘记创建一个按钮并将 Onclicklister 设置为它

  5. code the step2 in first activity inside button onclicklisten在按钮 onclicklisten 内的第一个活动中对 step2 进行编码

  6. now code the step3 in second activity现在在第二个活动中编码 step3

  7. now run the code现在运行代码

    ((where "name" is indicated as a token to verify so maintain same char in both activities)) ((其中“名称”表示为要验证的令牌,因此在两个活动中都保持相同的字符))

STEP1第1步

1.Mainactivity.this//(Activity from where you should get the value) 1.Mainactivity.this//(您应该从哪里获取值的活动)

EditText edittext=(EditText)findViewById(R.id.edittext);
Intent intent=new Intent(getApplicationContext(),Main2Activity.class);
intent.putExtra ( "name", ed1.getText().toString() );
startActivity(intent);

STEP2:第2步:

2.Main2activity//(Activity to where you should get the value) 2.Main2activity//(Activity到哪里你应该得到值)

Textview textview = (TextView) findViewById(R.id.textview);
Bundle bb;
bb=getIntent().getExtras();
textview.setText(bb.getString("name"));
public EditText var_name; 

var_name=(EditText)findViewById(R.id.input_id);

publc void function_name(){
    Intent i=new Intent(this, class_name.class);
    String s=this.edittext_var_name.getString().toString();
    i.putExtra("var_name","s");
    startActivity(i);
    }`

Hope this will help full for you.希望这对你有帮助。

This code for second activity where you want to show the Data from First Activity.此代码用于第二个活动,您要在其中显示第一个活动的数据。

 String mUrl = getIntent().getStringExtra("LinkingWord");
 editTextURL.setText(getIntent().getStringExtra("LinkingWord"));

Don't forget to call EditText ID不要忘记调用 EditText ID

  EditText editTextURL = findViewById(R.id.edit_url);

Easy Peasy十分简单

HappyCoding快乐编码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM