简体   繁体   中英

pass EditText to new Activity

I have in my main_activity.xml EditText and a Button .

Inside the EditText I insert a name and once I click the Button a new activity with new layout appears.

Now, I had like to use the value from the EditText inside my new activity ( hiscores.xml ) however the app crashes.

My MainActivity is as follows:

package com.example.test;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    private Button button;

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

        button = findViewById(R.id.continue_button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openHiScore();
            }
        });
    }

        public void openHiScore(){
            Intent intent = new Intent(this,HiScore.class);
            startActivity(intent);

        }

}

Then, inside HiScore the code is;

package com.example.test;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.EditText;


public class HiScore extends AppCompatActivity {

    EditText Name= (EditText)findViewById(R.id.username_text);
    String UserName = Name.getText().toString();

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

How do I correctly obtain the value inserted into the EditText to use it inside the string UserName ?

Thank you!

Use bundle to send data from one activity to another.Store data into the bundle and create an Intent to put an extra Bundle in order to pass it.

public void openHiScore(){
            Intent intent = new Intent(this,HiScore.class);
            Bundle b = new Bundle();
            b.putString("name", editText.getText().toString());
            intent.putExtras(b)
            startActivity(intent);

        }

In the HiScore class extract the data from the Bundle.

public class HiScore extends AppCompatActivity {

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

        Intent in = getIntent();
        // get the Bundle that stores the data of this Activity
        Bundle b = in.getExtras();

        // getting data from bundle
        String name = b.getString("name");
    }
}

To pass data from one activity to another, you will want to add the data as an extra to your intent .

    public void openHiScore(){
        Intent intent = new Intent(this,HiScore.class);
        intent.putExtra("NAME_INPUT",nameString)
        startActivity(intent);
    }

Then within the receiving activity, you want retrive the name from the intent :

   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.hiscore);
        String name = getIntent().getStringExtra("NAME_INPUT");    
    }

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