简体   繁体   中英

How to pass edittext value to another activity's edittext?

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

You can pass it using Intent's putExtra() method. 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

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 :

Sneder : 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

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

Based on Lucifer's answer you could use a TextView in the second activity:

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

PROCESS

  1. get two Activities(java classes)

  2. in first Activity take editview

  3. in second Activity take textview

  4. dont forget to create a button and set Onclicklister to it

  5. code the step2 in first activity inside button onclicklisten

  6. now code the step3 in second activity

  7. now run the code

    ((where "name" is indicated as a token to verify so maintain same char in both activities))

STEP1

1.Mainactivity.this//(Activity from where you should get the value)

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

STEP2:

2.Main2activity//(Activity to where you should get the value)

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 editTextURL = findViewById(R.id.edit_url);

Easy Peasy

HappyCoding

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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