AndroidのIntent戻り値
1955 ワード
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
one=(EditText) this.findViewById(R.id.one);
two=(EditText) this.findViewById(R.id.two);
result=(EditText) this.findViewById(R.id.result);
button=(Button)this.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String a=one.getText().toString();
String b=two.getText().toString();
Intent intent=new Intent(MainActivity.this,OtherActivity.class);
intent.putExtra("a", a);
intent.putExtra("b", b);
startActivityForResult(intent, 1);}});}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.i("Main", "requestCode:"+requestCode+"resultCode:"+resultCode);
if(resultCode==2){
if(requestCode==1){
result.setText(data.getStringExtra("tree"));} }
}
}
もう一つのactivity
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.other);
textView = (TextView) this.findViewById(R.id.msg);
editText = (EditText) this.findViewById(R.id.three);
button = (Button) this.findViewById(R.id.button2);
Intent intent = getIntent();
String a = intent.getStringExtra("a");
String b = intent.getStringExtra("b");
textView.setText(a + " + " + b + " =?");
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent=new Intent();
String tree=editText.getText().toString();
intent.putExtra("tree",tree);
setResult(2, intent);
finish();
}
});
}
}