Android中 requestCode与resultCode的区别与用法
requestCode:
用于判断传过来的数据来自于哪个activity
resultCode:
当一个Activity有多种数据传过来时,resultCode就可以用来判断传过来的数据的类型
下面用一个例子说明吧,首先看结果
说明:
三个按钮两个编辑框
点击单选联系人与单选回复有两个activity,分别选联系人与回复,这样就可以利用 requestCode来判断数据来自于那个activity
点击同时选,会出现两列数据,一列联系人,一列回复。选好联系人会跳转到上一个activity,把联系人填好,然后再点击按钮选好回复,自动跳转到上一个activity填好回复。(并不能算真正的同时选,只是为了理解resultCode的用法,来判断传过来的是联系人还是回复)
下面上代码吧:
# SimperActivity.java(MainActivity.java)
1 package com.example.lynn.adapter.Intent; 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 10 import com.example.lynn.adapter.R; 11 12 public class SimperActivity extends Activity { 13 14 final int NAME = 1; 15 final int SMS = 2; 16 //三个Activity的请求码 17 final int NAME_ACTIVITY = 0; 18 final int SMS_ACTIVITY = 3; 19 final int SELECT_ACTIVITY = 4; 20 private Button select; 21 private Button selectName; 22 private Button selectSms; 23 EditText nameText; 24 EditText smsText; 25 26 @Override 27 protected void onCreate(Bundle savedInstanceState) { 28 super.onCreate(savedInstanceState); 29 setContentView(R.layout.simper_layout); 30 select = (Button) findViewById(R.id.select); 31 selectName = (Button) findViewById(R.id.singer_name); 32 selectSms = (Button) findViewById(R.id.singer_sms); 33 nameText = (EditText) findViewById(R.id.name); 34 smsText = (EditText) findViewById(R.id.sms); 35 select.setOnClickListener(new View.OnClickListener() { 36 @Override 37 public void onClick(View v) { 38 Intent intent = new Intent(SimperActivity.this, SelectActivity.class); 39 startActivityForResult(intent, SELECT_ACTIVITY); 40 } 41 }); 42 } 43 44 45 @Override 46 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 47 super.onActivityResult(requestCode, resultCode, data); 48 String n, s; 49 switch (requestCode) { 50 case (NAME_ACTIVITY): 51 n = data.getStringExtra("name"); 52 nameText.setText(n); 53 break; 54 case (SMS_ACTIVITY): 55 s = data.getStringExtra("sms"); 56 smsText.setText(s); 57 break; 58 case (SELECT_ACTIVITY): { 59 if (resultCode == NAME) { 60 n = data.getStringExtra("name"); 61 nameText.setText(n); 62 } else if (resultCode == SMS) { 63 s = data.getStringExtra("sms"); 64 smsText.setText(s); 65 } 66 } 67 break; 68 } 69 70 } 71 72 //单选联系人的点击事件 73 public void toSelectName(View v) { 74 Intent intent = new Intent(SimperActivity.this, SelectPersonActivity.class); 75 startActivityForResult(intent, NAME_ACTIVITY); 76 77 } 78 79 //单选回复的点击事件 80 public void toSelectSms(View v) { 81 Intent intent = new Intent(SimperActivity.this, SelectSmsActivity.class); 82 startActivityForResult(intent, SMS_ACTIVITY); 83 84 } 85 }
simper_layout.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" android:layout_width="match_parent" 4 android:layout_height="match_parent"> 5 <TableLayout 6 android:layout_width="match_parent" 7 android:layout_height="wrap_content" 8 > 9 <TableRow> 10 <TextView 11 android:layout_width="0dp" 12 android:layout_height="wrap_content" 13 android:layout_weight="1" 14 android:text="@string/name" 15 /> 16 <EditText 17 android:id="@+id/name" 18 android:labelFor="@id/name" 19 android:inputType="text" 20 android:layout_width="0dp" 21 android:layout_height="wrap_content" 22 android:layout_weight="5" 23 /> 24 </TableRow> 25 <TableRow> 26 <TextView 27 android:layout_width="0dp" 28 android:layout_height="wrap_content" 29 android:layout_weight="1" 30 android:text="@string/sms" 31 /> 32 <EditText 33 android:id="@+id/sms" 34 android:labelFor="@id/sms" 35 android:inputType="text" 36 android:layout_width="0dp" 37 android:layout_height="wrap_content" 38 android:layout_weight="5" 39 /> 40 </TableRow> 41 </TableLayout> 42 43 44 <Button 45 android:id="@+id/select" 46 android:layout_width="wrap_content" 47 android:layout_height="wrap_content" 48 android:text="同时选好" 49 /> 50 <Button 51 android:id="@+id/singer_name" 52 android:layout_width="wrap_content" 53 android:layout_height="wrap_content" 54 android:text="单选联系人" 55 android:onClick="toSelectName" 56 /> 57 <Button 58 android:id="@+id/singer_sms" 59 android:layout_width="wrap_content" 60 android:layout_height="wrap_content" 61 android:text="单选回复" 62 android:onClick="toSelectSms" 63 /> 64 </LinearLayout>
SelectActivity.java(同时选)
1 package com.example.lynn.adapter.Intent; 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.AdapterView; 8 import android.widget.ArrayAdapter; 9 import android.widget.ListView; 10 import android.widget.Toast; 11 12 import com.example.lynn.adapter.R; 13 14 import java.util.ArrayList; 15 import java.util.List; 16 17 18 public class SelectActivity extends Activity { 19 private List<String> nameList; 20 private List<String> smsList; 21 22 private ListView nameView; 23 private ListView smsView; 24 25 @Override 26 protected void onCreate(Bundle savedInstanceState) { 27 super.onCreate(savedInstanceState); 28 setContentView(R.layout.select_layout); 29 nameView= (ListView) findViewById(R.id.allpersons); 30 smsView= (ListView) findViewById(R.id.allsms); 31 addContent(); 32 nameView.setAdapter(new ArrayAdapter<String>(this, 33 android.R.layout.simple_list_item_1,nameList)); 34 smsView.setAdapter(new ArrayAdapter<String>(this, 35 android.R.layout.simple_list_item_1,smsList)); 36 nameView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 37 @Override 38 public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 39 Intent data=new Intent(); 40 data.putExtra("name", nameList.get(position)); 41 setResult(1,data); 42 Toast.makeText(SelectActivity.this, "您选择了"+nameList.get(position)+" !", Toast.LENGTH_LONG).show(); 43 finish(); 44 45 46 } 47 }); 48 smsView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 49 @Override 50 public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 51 Intent data=new Intent(); 52 data.putExtra("sms", smsList.get(position)); 53 setResult(2,data); 54 finish(); 55 } 56 }); 57 } 58 59 //添加数据 60 public void addContent(){ 61 nameList=new ArrayList<>(); 62 nameList.add("lynn"); 63 nameList.add("jack"); 64 nameList.add("cat"); 65 66 smsList=new ArrayList<>(); 67 smsList.add("waiting..."); 68 smsList.add("I am busy..."); 69 smsList.add("hey,man...."); 70 71 } 72 73 74 }
select_layout.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="horizontal"> 6 7 <LinearLayout 8 android:layout_width="0dp" 9 android:layout_height="wrap_content" 10 android:layout_weight="1" 11 android:orientation="vertical"> 12 13 <TextView 14 android:layout_width="wrap_content" 15 android:layout_height="wrap_content" 16 android:text="联系人;" 17 android:textSize="24dp" /> 18 19 <ListView 20 android:id="@+id/allpersons" 21 android:layout_width="wrap_content" 22 android:layout_height="wrap_content" /> 23 </LinearLayout> 24 25 <LinearLayout 26 android:layout_width="0dp" 27 android:layout_height="wrap_content" 28 android:layout_weight="1" 29 android:orientation="vertical"> 30 31 <TextView 32 android:layout_width="wrap_content" 33 android:layout_height="wrap_content" 34 android:text="回复:" 35 android:textSize="24dp" /> 36 37 <ListView 38 android:id="@+id/allsms" 39 android:layout_width="wrap_content" 40 android:layout_height="wrap_content" 41 42 /> 43 </LinearLayout> 44 </LinearLayout>
SelectSmsActivity.java(单选回复)
1 package com.example.lynn.adapter.Intent; 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.AdapterView; 8 import android.widget.ArrayAdapter; 9 import android.widget.ListView; 10 11 import com.example.lynn.adapter.R; 12 13 /** 14 * Created by Administrator on 2016/5/29. 15 */ 16 public class SelectSmsActivity extends Activity { 17 18 private String[] allsms={"I am busy....","waiting....","hey,man.."}; 19 private ListView smsView; 20 @Override 21 protected void onCreate(Bundle savedInstanceState) { 22 super.onCreate(savedInstanceState); 23 setContentView(R.layout.allsms_layout); 24 smsView= (ListView) findViewById(R.id.the_sms); 25 smsView.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,allsms)); 26 smsView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 27 @Override 28 public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 29 Intent data=new Intent(); 30 data.putExtra("sms",allsms[position]); 31 setResult(3,data); 32 finish(); 33 } 34 }); 35 36 } 37 }
SelectPersonActivity.java(单选联系人)
1 package com.example.lynn.adapter.Intent; 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.AdapterView; 8 import android.widget.ArrayAdapter; 9 import android.widget.ListView; 10 11 import com.example.lynn.adapter.R; 12 13 import java.util.List; 14 15 16 public class SelectPersonActivity extends Activity{ 17 18 private String[] allNames=new String[]{"lynn","jack","cat"}; 19 private ListView nameView; 20 21 @Override 22 protected void onCreate(Bundle savedInstanceState) { 23 super.onCreate(savedInstanceState); 24 setContentView(R.layout.allnames_layout); 25 nameView= (ListView) findViewById(R.id.the_name); 26 nameView.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,allNames)); 27 nameView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 28 @Override 29 public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 30 Intent data=new Intent(); 31 data.putExtra("name",allNames[position]); 32 setResult(0,data); 33 finish(); 34 } 35 }); 36 } 37 }
说明:以上的两个Activity的布局文件都是只有一个listView,故此处不再给出,有不对的地方多多指教!
------------------------
没有谁的人生不是斩棘前行
----------------------------------------
JM