在我的项目中,ActivityTwo 是作为 ActivityOne 的子活动启动的,而 ActivityOne 又经过修改以处理从 ActivityTwo 返回的数据。
ActivityOne 中的 TextView 应检索作为 ActivityTwo 中的键值添加到意图对象的 EditText 中输入的文本。尽管成功启动,但它并没有提取意图对象。
我查看了可能面临的类似问题并解决了这个问题: How to manage `startActivityForResult` on Android?
然而,我仍然无法弄清楚为什么我的 TextView 无法设置从第二个活动中检索到的意图对象中提取的文本。
任何有用的帮助表示赞赏。
在此先感谢您可以在下面看到代码:
活动一:
public class ActivityOne extends AppCompatActivity {
private static final int REQUEST_CODE = 1;
EditText isimAlani, soyadiAlani;
TextView tlfNo;
Button btnIleri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_one);
isimAlani = (EditText) findViewById(R.id.isimalani);
soyadiAlani = (EditText) findViewById(R.id.soyadialani);
tlfNo = (TextView) findViewById(R.id.tlfno);
btnIleri = (Button) findViewById(R.id.btnileri);
btnIleri.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(ActivityOne.this, ActivityTwo.class);
i.putExtra("isim", isimAlani.getText().toString());
i.putExtra("soyisim", soyadiAlani.getText().toString());
startActivityForResult(i, REQUEST_CODE);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if ((requestCode == REQUEST_CODE) && (requestCode == RESULT_OK)) {
String result = data.getExtras().getString("returnData");
tlfNo.setText("Telefon numaranız: " + result);
}
}
}
活动二:
public class ActivityTwo extends AppCompatActivity {
EditText tlfNoAlani;
TextView tvAdSoyad;
Button btnGeri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_two);
tlfNoAlani = (EditText) findViewById(R.id.tlfnoalani);
tvAdSoyad = (TextView) findViewById(R.id.tvadsoyad);
btnGeri = (Button) findViewById(R.id.btngeri);
Bundle extras = getIntent().getExtras();
if (extras != null) {
tvAdSoyad.setText("Merhaba " + extras.getString("isim") + " " + extras.getString("soyisim"));
}
btnGeri.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent data = new Intent();
String returnString = tlfNoAlani.getText().toString();
data.putExtra("returnData", returnString);
setResult(RESULT_OK, data);
finish();
}
});
}
}