Intent intent = new Intent(this,Name.class);
intent.putExtra("key", et.getText().toString());
startActivity(intent);
Intent intent = getIntent();
String str = intent.getStringExtra("key");
tv.setText(str);
使用上面的代碼可以從其它activity中獲取字符串值,但是在另一個activity中我需要editText 對象。如何把EditText傳遞到另外一個activity?
為什麼不發送ID,然後在 receiver Activity 上使用 findViewByid():
Intent intent = new Intent();
String name="textView";
int value=EditText.getID();
intent.putExtra(name, value);
setResult(RESULT_OK, intent);
在receiver Activity:
private static final int REQUEST_CODE=1;
private int id;
private EditText et;
private Intent i;
i = new Intent(this,Name.class);
startActivityForResult(intent, REQUEST_CODE);
et = (EditText)findViewById(id);
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode == RESULT_OK && requestCode == REQUEST_CODE)
{
if (data.hasExtra("textView"))
{
//Get the selected file.
id = data.getExtras().getInt("textView");
}
}
}