我是剛入門的菜鳥,求各位大神幫忙看看,以下是我操作是的界面截圖和代碼,跪求幫!!!
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
int a = Integer.parseInt(one.getText().toString());
int b = Integer.parseInt(two.getText().toString());
Intent intent = new Intent(MainActivity.this,OtherActivity.class);
intent.putExtra("a", a);
intent.putExtra("b", b);
startActivity(intent);
startActivityForResult(intent, REQUESTCODE); //返回結果
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(OtherActivity.this, MainActivity.class);
int three = Integer.parseInt(editText.getText().toString());
intent.putExtra("three", three);
//通過Intent對象返回結果,setResult方法
setResult(1,intent);
finish(); //結束當前activity的生命周期。
}
});
protected void onActivityResult(int arg0, int arg1, Intent arg2) {
// TODO Auto-generated method stub
super.onActivityResult(arg0, arg1, arg2);
if (arg1 == 1) {
if (arg0 == REQUESTCODE) {
int thre = arg2.getIntExtra("three", 0);
result.setText(String.valueOf(thre));
}
}
}
仔細看代碼:
startActivity(intent);
startActivityForResult(intent, REQUESTCODE); //返回結果
去掉第一行那個startActivity應該就可以了