1.在main.xml文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="@+id/one"
android:layout_width="90dp"
android:layout_height="wrap_content" >
</EditText>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+" >
</TextView>
<EditText
android:id="@+id/two"
android:layout_width="90dp"
android:layout_height="wrap_content" >
</EditText>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="=" >
</TextView>
<EditText
android:id="@+id/res"
android:layout_width="90dp"
android:layout_height="wrap_content" >
</EditText>
</LinearLayout>
<Button
android:id="@+id/btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="计算结果" >
</Button>
</LinearLayout>
2.新增一个XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/msg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
></TextView>
<EditText
android:id="@+id/three"
android:layout_width="match_parent"
android:layout_height="wrap_content"
></EditText>
</LinearLayout>
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="返回结果"
></Button>
</LinearLayout>
3.MainActivity
public class MainActivity extends Activity {
private Button button;
private final static int REQUESTCODE = 1;
private EditText one,two,res;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) this.findViewById(R.id.btn);
one = (EditText) this.findViewById(R.id.one);
two = (EditText) this.findViewById(R.id.two);
res = (EditText) this.findViewById(R.id.res);
button.setOnClickListener(new View.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 there = data.getIntExtra("there", 0);
res.setText(String.valueOf(there));
}
}
}
}
4.OtherActivity
public class OtherActivity extends Activity {
private Button button;
private TextView textView;
private EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_other);
button = (Button) this.findViewById(R.id.button2);
textView = (TextView) this.findViewById(R.id.msg);
editText = (EditText) this.findViewById(R.id.three);
Intent intent = getIntent();
int a = intent.getIntExtra("a", 0);
int b = intent.getIntExtra("b", 0);
textView.setText(a+" + "+b+" = "+" ? ");
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
int three = Integer.parseInt(editText.getText().toString());
intent.putExtra("there", three);
setResult(2, intent);
finish();
}
});
}
}
5.清单文件<activity android:name=".OtherActivity"></activity>