MainActivity.java
package com.example.android_intent_result;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText one, two, result;
private Button computeResult;
private final static int REQUESTCODE = 1; // 返回结果码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
one = (EditText) this.findViewById(R.id.one);
two = (EditText) this.findViewById(R.id.two);
result = (EditText) this.findViewById(R.id.result);
computeResult = (Button) this.findViewById(R.id.computeResult);
computeResult.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 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);
startActivityForResult(intent, REQUESTCODE); // 表示可以返回结果
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == 2) {// 两边的结果码保持一致
if (requestCode == REQUESTCODE) {
int three = data.getIntExtra("three", 0);
Toast.makeText(MainActivity.this, "RESULT_OK" + RESULT_OK,
Toast.LENGTH_SHORT).show();
result.setText(String.valueOf(three));
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
OtherActivity.java
package com.example.android_intent_result;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class OtherActivity extends Activity {
private Button button;
private TextView show;
private EditText three;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_other);
show = (TextView) this.findViewById(R.id.show);
three = (EditText) this.findViewById(R.id.three);
button = (Button) this.findViewById(R.id.returnResult);
Intent intent = getIntent();
int a = intent.getIntExtra("a", 0);
int b = intent.getIntExtra("b", 0);
show.setText(a + " + " + b + " = ?");
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
int threeNum = Integer.parseInt(three.getText().toString());
intent.putExtra("three", threeNum);
// 通过intent对象返回结果,setResult方法
setResult(2, intent);// 两边的结果码保持一致
finish(); // 借宿当前Activity生命周期
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.other, menu);
return true;
}
}