简体   繁体   中英

Get text from EditText into string to another activity

I've been searching and I don't find the way. The solutions I found doesn't work or I don't know how to implement them.

My Main Activity

public class MainActivity extends AppCompatActivity {



@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        EditText txtDescription = (EditText) findViewById(R.id.nameE);
        final String nameE2 = txtDescription.getText().toString();

        Button bVerify = (Button) findViewById(R.id.verifyB);
        bVerify.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                /** Called when the user clicks the Next button */
                Log.d("EditText", nameE2);
                Intent intent = new Intent(MainActivity.this, ResultActivity.class);
                intent.putExtra(EXTRA_MESSAGE, nameE2);
                startResultActivity(null);
            }
        });
    }

    public void startResultActivity(View view){
        Intent sra = new Intent(this, ResultActivity.class);
        startActivity(sra);

    }
    }

This, already in my code, are the 2 solutions I found everywere, but I don't know how to use them:

 EditText txtDescription = (EditText) findViewById(R.id.nameE);
    String nameE2 = txtDescription.getText().toString();

&

Log.d("EditText", nameE2);
        Intent intent = new Intent(MainActivity.this, ResultActivity.class);
        intent.putExtra(EXTRA_MESSAGE, nameE2);

Inside activity_main, among other things, I've this EditText and this button:

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="textPersonName"
    android:text="Name"
    android:ems="10"
    android:id="@+id/nameE"
    android:layout_below="@+id/NameTxt"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:editable="false" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/verifyB"
    android:layout_alignParentStart="true"
    android:text="@string/verifyB"
    android:layout_alignParentEnd="true"
    android:layout_below="@+id/nameE"
    android:layout_alignParentLeft="true" />

In the "ractivity" layout I've this PlainText. In this one I want to add the EditText input from the other activity/layout.

<TextView

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello  + nameE2 + . Are you sure you want to accept these terms?"
    android:id="@+id/textView"
    android:textSize="20dp"
    android:textColor="@color/abc_input_method_navigation_guard" />

"nameE2" must be the string of the EditText.

I would love to know how to solve it.

Thanks in advance. If you need additional data, tell me.

Use

 startActivity(intent); 

instead of

 startResultActivity(null);

in your Button onClick

This seems fine, with a little modification.

bVerify.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        /** Called when the user clicks the Next button */
        Log.d("EditText", nameE2);
        Intent intent = new Intent(MainActivity.this, ResultActivity.class);
        intent.putExtra(EXTRA_MESSAGE, nameE2);
        startResultActivity(null);
    }
});

Here instead of pass null to startResultActivity() , pass the created Intent similar to this:

bVerify.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        final String nameE2 = txtDescription.getText().toString();
        /** Called when the user clicks the Next button */
        Log.d("EditText", nameE2);
        Intent intent = new Intent(MainActivity.this, ResultActivity.class);
        intent.putExtra(EXTRA_MESSAGE, nameE2);
        startResultActivity(intent);
    }
});

And the startResultActivity() method have to look like this:

public void startResultActivity(Intent intent){

    startActivity(intent);

}

EDIT: As I mentioned, you cannot use your variable directly in your layout file.

You have to set it dynamically. You can do that in your ResultActivity's onCreate() method. It have to look similar to this:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.ractivity); // your second layout file

    TextView textView = (TextView) findViewById(R.id.textView); 
    String stringResult = getIntent().getStringExtra(EXTRA_MESSAGE);
    String textViewText = "Hello "+ stringResult +". Are you sure you want to accept these terms?";
    textView.setText(textViewText);

}

This will work if in your second layout file there is a Textview with textView id.

Try this code :

 public class MainActivity extends AppCompatActivity {

        @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);

                EditText txtDescription = (EditText) findViewById(R.id.nameE);
                final String nameE2 = txtDescription.getText().toString();

                Button bVerify = (Button) findViewById(R.id.verifyB);
                bVerify.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        /** Called when the user clicks the Next button */
                        Log.d("EditText", nameE2);
                        Intent intent = new Intent(MainActivity.this, ResultActivity.class);
                        intent.putExtra(EXTRA_MESSAGE, nameE2);
                        startActivity(intent);
                    }
                });
            }


            }

changed onClickListener to

bVerify.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            /** Called when the user clicks the Next button */
            final String nameE2 = txtDescription.getText().toString();
            Log.d("EditText", nameE2);
            Intent intent = new Intent(MainActivity.this, ResultActivity.class);
            intent.putExtra(EXTRA_MESSAGE, nameE2);
            startActivity(intent);
        }
});

you are getting text at start when it does not have any text so move

final String nameE2 = txtDescription.getText().toString();

inside onClick of bVerify view so it will read current text when client will click on button

Change startResultActivity(null); into startActivity(intent); . And then in the other Activity where you need to use that String:

Intent intent = getIntent();
String getExtra = intent.getStringExtra("EXTRA_MESSAGE");
TextView.setText(getExtra);

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