简体   繁体   中英

Taken value from edittext and passing it as a string to another activity

Im looking to take the value from a edittext field and pass it to another activity as a string. I have done below as shown. I think its working but I would like be to able to print the text out to see if the variable value actually passed over

But once I type Log.d("PJM",value); the activty closes with a error

MainScreen Activity

public class MainScreen extends Activity {

Button btnSearch, btnFavourites;
EditText enterDefinition;
Editable word;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_screen);

    btnSearch=(Button)findViewById(R.id.btnSearch);
    btnFavourites=(Button)findViewById(R.id.btnFavourites);
    enterDefinition=(EditText)findViewById(R.id.enterDefinition);

    String text = enterDefinition.getText().toString();

    Intent i = new Intent(this, Definition.class);
    i.putExtra("KEY",text);
    //startActivity(i);

    //Intent i = new Intent(this, Definition.class);
    //i.putExtra("text_Label",theText);
    //startActivity(i);

Definition Activity

public class Definition extends Activity {

Button btnSend, btnSaveFave, btnReturn;
EditText enterDefinition;
String value;
TextView definition;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_definition);

    btnSend=(Button)findViewById(R.id.btnSend);
    btnSaveFave=(Button)findViewById(R.id.btnSaveFave);
    btnReturn=(Button)findViewById(R.id.btnReturn);
    enterDefinition=(EditText)findViewById(R.id.enterDefinition);
    definition=(TextView)findViewById(R.id.tvDefinition);

    definition.setText("some text");

    Bundle extras = getIntent().getExtras();
    String value = null; 
    if (extras != null) {
      value = extras.getString("KEY");
    }
    else {
      value = "None";
    }
    Log.d("text entered", value);
    Toast.makeText(getBaseContext(), value, Toast.LENGTH_LONG).show();

You want:

String value = null; 
if (extras != null) {
  value = extras.getString("KEY");
}
else {
  value = "None"
}
Log.d("PJM", value);

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