开发者

Change widget text in another activity

开发者 https://www.devze.com 2022-12-25 21:49 出处:网络
Suppose I have ActivityA and ActivityB, also suppose that ActivityA is active. I need to: Programmatically set a text of EditText in ActivityB from ActivityA

Suppose I have ActivityA and ActivityB, also suppose that ActivityA is active. I need to:

  1. Programmatically set a text of EditText in ActivityB from ActivityA
  2. Launch ActivityB

Here's my code:

EditText res;
final LayoutInflater factory = getLayoutInflater();
final View resultView = factory.inflate(R.layout.ActivityB, null);

// get widget
res = (EditText) resultView.findViewById(R.id.txtResult);

// set the text
res.setText("foobar");


// create intent
Intent i = new Intent(ActivityA.this, ActivityB.class);
startActivity(i);

ActivityB starts, but without 开发者_高级运维any text in txtResult. How can I fix that?


Before startActivity(i); in ActivityA

i.putExtra("myText", "someValue");

In ActivityB's onCreate

String str = getIntent().getStringExtra("myText");
myEditText.setText(str);
0

精彩评论

暂无评论...
验证码 换一张
取 消