Passing value from a EditText to TextView

前端 未结 1 1192
春和景丽
春和景丽 2021-01-27 13:27

I want to take the value from a EditText from one page and to display that value to a TextView in another page. How can I do that? How can I store values in EditText? Plesae Hel

1条回答
  •  长情又很酷
    2021-01-27 13:53

    When you mention "on a different page" you mean in a new activity right?

    Assuming thats what you mean, you must simply cast the content of the EditText into a bundle and sent it to the next activity. Here is a sample to get you started.

    This codes should be in the method you are using to start the new activity.

    For the first activity:

    EditText txtTitle = (EditText)findViewById(R.id.txtTitle);
    Intent i = new Intent(this, com.dzinesunlimited.quotetogo.Step2.class);
    Bundle bundle = new Bundle();
    bundle.putString("title", txtTitle.getText().toString());
    i.putExtras(bundle);
    startActivity(i);
    

    For the second activity

    This code, in my case is in the onCreate() method.

    Bundle bundle = this.getIntent().getExtras();
    String title = bundle.getString("title");
    
    ((TextView)findViewById(R.id.summaryTitle)).setText(title);
    

    Hope this helps.

    0 讨论(0)
提交回复
热议问题