请求码和结果码
一、简介
请求码:
例如请求页面有多个button,根据请求码就知道是哪个button在请求
结果码:
多个请求可以打开多个页面,根据结果码就知道我们打开的是哪个界面
请求码是用来标识请求源的,结果码是用来标识结果源的。
二、具体步骤
这里演示结果码的
1、界面1里面的结果码是100
setResult(100, intent);
2、界面2里面的结果码是200
setResult(200, intent);
3、在主界面的 onActivityResult方法中根据结果码判断数据来源于哪个页面
switch (resultCode) {
case 100:
et_phoneNumber.setText(bundle.getString("phoneNumber"));
break;
case 200:
et_phoneNumber2.setText(bundle.getString("phoneNumber2"));
break;
default:
break;
}
三、代码实例
下面的代码时返回值,实质和用intent传值是一样的,都是getExtras和putExtras
这里是测试结果码,请求码的测试在代码的注释里面有
结果图:
打开第一个按钮进入一个界面,打开第二个按钮进入另外一个界面
代码:
activityReturnData.MainActivity
1 package activityReturnData; 2 3 4 5 6 import com.example.activityReturnData.R; 7 8 import android.app.Activity; 9 import android.content.Intent; 10 import android.os.Bundle; 11 import android.view.View; 12 import android.view.View.OnClickListener; 13 import android.widget.Button; 14 import android.widget.EditText; 15 16 17 18 public class MainActivity extends Activity{ 19 private Button btn_chooseContacter;//创建一个button对象 20 private EditText et_phoneNumber; 21 private Button btn_chooseContacter2;//创建一个button对象 22 private EditText et_phoneNumber2; 23 protected void onCreate(Bundle savedInstanceState) { 24 super.onCreate(savedInstanceState);//父类操作 25 setContentView(R.layout.activity_main);//引入名为activity_main的界面 26 btn_chooseContacter=(Button) findViewById(R.id.btn_chooseContacter);//找id为btn_openActivity的button 27 et_phoneNumber=(EditText) findViewById(R.id.et_phoneNum); 28 btn_chooseContacter.setOnClickListener(new OnClickListener() {//设置button点击监听 29 30 @Override 31 public void onClick(View v) {//onclick事件 32 // TODO Auto-generated method stub 33 Intent intent=new Intent(MainActivity.this,Activity01.class);//初始化intent 34 //请求码:resultCode 35 startActivityForResult(intent, 1); 36 } 37 }); 38 39 40 btn_chooseContacter2=(Button) findViewById(R.id.btn_chooseContacter2);//找id为btn_openActivity的button 41 et_phoneNumber2=(EditText) findViewById(R.id.et_phoneNum2); 42 btn_chooseContacter2.setOnClickListener(new OnClickListener() {//设置button点击监听 43 44 @Override 45 public void onClick(View v) {//onclick事件 46 // TODO Auto-generated method stub 47 Intent intent=new Intent(MainActivity.this,Activity02.class);//初始化intent 48 //请求码:resultCode 49 startActivityForResult(intent, 2); 50 } 51 }); 52 53 } 54 /* 55 * Activity01调用完finish()方法销毁之后,就会调用MainActivity的onActivityResult方法 56 * 因为Activity01完成后会返回调用它的方法 57 * (non-Javadoc) 58 * @see android.app.Activity#onActivityResult(int, int, android.content.Intent) 59 */ 60 @Override 61 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 62 // TODO Auto-generated method stub 63 super.onActivityResult(requestCode, resultCode, data); 64 //就是如果没有传值回来的情况,直接return就ok了 65 if(data==null) return; 66 Bundle bundle=data.getExtras(); 67 String phoneNumber=bundle.getString("phoneNumber"); 68 69 // switch (requestCode) { 70 // case 1: 71 // et_phoneNumber.setText(phoneNumber); 72 // break; 73 // case 2: 74 // et_phoneNumber2.setText(phoneNumber); 75 // break; 76 // default: 77 // break; 78 // } 79 80 switch (resultCode) { 81 case 100: 82 et_phoneNumber.setText(bundle.getString("phoneNumber")); 83 break; 84 case 200: 85 et_phoneNumber2.setText(bundle.getString("phoneNumber2")); 86 break; 87 default: 88 break; 89 } 90 91 } 92 }
activityReturnData.Activity01
1 package activityReturnData; 2 3 4 import com.example.activityReturnData.R; 5 6 import android.app.Activity; 7 import android.content.Intent; 8 import android.os.Bundle; 9 import android.view.View; 10 import android.view.View.OnClickListener; 11 import android.widget.TextView; 12 13 public class Activity01 extends Activity{ 14 private TextView tv_phoneNum; 15 @Override 16 protected void onCreate(Bundle savedInstanceState) { 17 // TODO Auto-generated method stub 18 super.onCreate(savedInstanceState); 19 setContentView(R.layout.activity01); 20 tv_phoneNum=(TextView) findViewById(R.id.tv_phoneNum); 21 tv_phoneNum.setOnClickListener(new OnClickListener() { 22 23 @Override 24 public void onClick(View v) { 25 // TODO Auto-generated method stub 26 Intent intent=new Intent(); 27 intent.putExtra("phoneNumber",tv_phoneNum.getText()); 28 //resultCode,结果码 29 setResult(100, intent); 30 finish(); 31 } 32 }); 33 } 34 }
activityReturnData.Activity02
1 package activityReturnData; 2 3 4 import com.example.activityReturnData.R; 5 6 import android.app.Activity; 7 import android.content.Intent; 8 import android.os.Bundle; 9 import android.view.View; 10 import android.view.View.OnClickListener; 11 import android.widget.TextView; 12 13 public class Activity02 extends Activity{ 14 private TextView tv_phoneNum2; 15 @Override 16 protected void onCreate(Bundle savedInstanceState) { 17 // TODO Auto-generated method stub 18 super.onCreate(savedInstanceState); 19 setContentView(R.layout.activity02); 20 tv_phoneNum2=(TextView) findViewById(R.id.tv_phoneNum2); 21 tv_phoneNum2.setOnClickListener(new OnClickListener() { 22 23 @Override 24 public void onClick(View v) { 25 // TODO Auto-generated method stub 26 Intent intent=new Intent(); 27 intent.putExtra("phoneNumber2",tv_phoneNum2.getText()); 28 //resultCode,结果码 29 setResult(200, intent); 30 finish(); 31 } 32 }); 33 } 34 }
/activityReturnData/AndroidManifest.xml
1 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 2 package="com.example.activityReturnData" 3 android:versionCode="1" 4 android:versionName="1.0" > 5 6 <uses-sdk 7 android:minSdkVersion="8" 8 android:targetSdkVersion="19" /> 9 10 <application 11 android:allowBackup="true" 12 android:icon="@drawable/ic_launcher" 13 android:label="@string/app_name" 14 android:theme="@style/AppTheme" > 15 <activity 16 android:name="activityReturnData.MainActivity" 17 android:label="@string/app_name" > 18 <intent-filter> 19 <action android:name="android.intent.action.MAIN" /> 20 21 <category android:name="android.intent.category.LAUNCHER" /> 22 </intent-filter> 23 </activity> 24 <activity android:name="activityReturnData.Activity01" android:exported="true"></activity> 25 <activity android:name="activityReturnData.Activity02" android:exported="true"></activity> 26 </application> 27 28 </manifest>
/activityReturnData/res/layout/activity_main.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 7 <EditText 8 android:id="@+id/et_phoneNum" 9 android:layout_width="match_parent" 10 android:layout_height="wrap_content" 11 android:ems="10" > 12 13 <requestFocus /> 14 </EditText> 15 16 <Button 17 android:id="@+id/btn_chooseContacter" 18 android:layout_width="318dp" 19 android:layout_height="50dp" 20 android:text="@string/btn_chooseContacter" /> 21 22 <EditText 23 android:id="@+id/et_phoneNum2" 24 android:layout_width="match_parent" 25 android:layout_height="wrap_content" 26 android:ems="10" > 27 28 <requestFocus /> 29 </EditText> 30 31 <Button 32 android:id="@+id/btn_chooseContacter2" 33 android:layout_width="318dp" 34 android:layout_height="50dp" 35 android:text="@string/btn_chooseContacter" /> 36 37 </LinearLayout>