MainActivity.java
1 package com.example.returnresult; 2 3 import android.os.Bundle; 4 import android.view.View; 5 import android.widget.Button; 6 import android.widget.EditText; 7 import android.app.Activity; 8 import android.content.Intent; 9 10 public class MainActivity extends Activity { 11 12 private EditText num1, num2, result; 13 private Button button; 14 private final static int REQUESTCODE = 1;// 返回的结果码 15 16 protected void onCreate(Bundle savedInstanceState) { 17 super.onCreate(savedInstanceState); 18 setContentView(R.layout.activity_main); 19 20 num1 = (EditText) this.findViewById(R.id.num1); 21 num2 = (EditText) this.findViewById(R.id.num2); 22 result = (EditText) this.findViewById(R.id.result); 23 button = (Button) this.findViewById(R.id.count); 24 25 button.setOnClickListener(new View.OnClickListener() { 26 27 public void onClick(View v) { 28 int i1 = Integer.parseInt(num1.getText().toString()); 29 int i2 = Integer.parseInt(num2.getText().toString()); 30 Intent intent = new Intent(MainActivity.this, Other.class); 31 intent.putExtra("i1", i1); 32 intent.putExtra("i2", i2); 33 startActivityForResult(intent, REQUESTCODE);// 表示可以返回结果 34 35 } 36 }); 37 } 38 39 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 40 // TODO Auto-generated method stub 41 super.onActivityResult(requestCode, resultCode, data); 42 if (resultCode == 2) { 43 if (requestCode == REQUESTCODE) { 44 int three = data.getIntExtra("three", 0); 45 result.setText(String.valueOf(three)); 46 } 47 } 48 } 49 }
Other.java
1 package com.example.returnresult; 2 3 import android.app.Activity; 4 import android.content.Intent; 5 import android.os.Bundle; 6 import android.view.View; 7 import android.widget.Button; 8 import android.widget.EditText; 9 import android.widget.TextView; 10 11 public class Other extends Activity { 12 13 private Button button; 14 private TextView textView; 15 private EditText editText; 16 @Override 17 protected void onCreate(Bundle savedInstanceState) { 18 // TODO Auto-generated method stub 19 super.onCreate(savedInstanceState); 20 setContentView(R.layout.other); 21 button = (Button)this.findViewById(R.id.rt); 22 textView = (TextView)this.findViewById(R.id.msg); 23 editText = (EditText)this.findViewById(R.id.three); 24 Intent intent = getIntent(); 25 int a = intent.getIntExtra("i1", 0); 26 int b = intent.getIntExtra("i2", 0); 27 textView.setText(a+" + "+b+" = "+" ? "); 28 button.setOnClickListener(new View.OnClickListener() { 29 @Override 30 public void onClick(View v) { 31 // TODO Auto-generated method stub 32 Intent intent = new Intent(); 33 int three = Integer.parseInt(editText.getText().toString()); 34 intent.putExtra("three", three); 35 //通过Intent对象返回结果,setResult方法, 36 setResult(2, intent); 37 finish();//结束当前的Activity的生命周期。 38 } 39 }); 40 } 41 }
MainActivity.xml
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="fill_parent" 4 android:layout_height="fill_parent" 5 android:orientation="vertical" 6 tools:context=".MainActivity" > 7 8 <LinearLayout 9 xmlns:tools="http://schemas.android.com/tools" 10 android:layout_width="fill_parent" 11 android:layout_height="wrap_content" 12 android:orientation="horizontal" > 13 14 <EditText 15 android:id="@+id/num1" 16 android:layout_width="95dp" 17 android:layout_height="wrap_content" 18 android:inputType="number" /> 19 20 <TextView 21 android:id="@+id/plus" 22 android:layout_width="wrap_content" 23 android:layout_height="wrap_content" 24 android:text="@string/plus" /> 25 26 <EditText 27 android:id="@+id/num2" 28 android:layout_width="95dp" 29 android:layout_height="wrap_content" 30 android:inputType="number" /> 31 32 <TextView 33 android:id="@+id/equal" 34 android:layout_width="wrap_content" 35 android:layout_height="wrap_content" 36 android:text="@string/equal" /> 37 <EditText 38 android:id="@+id/result" 39 android:layout_width="95dp" 40 android:layout_height="wrap_content" 41 android:inputType="number" 42 /> 43 </LinearLayout> 44 45 <Button 46 android:id="@+id/count" 47 android:layout_width="match_parent" 48 android:layout_height="wrap_content" 49 android:text="@string/计算结果" > 50 </Button> 51 52 </LinearLayout>
Other.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="fill_parent" 4 android:layout_height="fill_parent" 5 android:orientation="vertical" > 6 7 <LinearLayout 8 xmlns:android="http://schemas.android.com/apk/res/android" 9 android:layout_width="fill_parent" 10 android:layout_height="wrap_content" 11 android:orientation="vertical" > 12 13 <TextView 14 android:id="@+id/msg" 15 android:layout_width="match_parent" 16 android:layout_height="wrap_content" > 17 </TextView> 18 19 <EditText 20 android:id="@+id/three" 21 android:layout_width="match_parent" 22 android:layout_height="wrap_content" 23 android:inputType="number" > 24 </EditText> 25 </LinearLayout> 26 27 <Button 28 android:id="@+id/rt" 29 android:layout_width="match_parent" 30 android:layout_height="wrap_content" 31 android:text="@string/rt" > 32 </Button> 33 34 </LinearLayout>