startActivityForResult用于启动其他指定的Activity,而且期望获取该指定Activity返回的结果。
启动其他Activity并返回结果过程:
当前ActivityA需要调用startActivityForResult(int requestCode,Intent intent)请求调用参数intent对应的ActivityB,请求码是requestCode,同时需要重写onActivityResult(int requestCode,int resultCode,Intent intent),当被启动的ActivityB返回结果时,必须使用setResult(int resultCode,Intent intent)将携带数据的intent传回到ActivityA,这个传回动作响应的结果码是resultCode,此时会触发onActivityResult,然后请求码和结果码就唯一确定一个过程,然后就可以接下来处理了
请求码和结果码
一个Activity中可能包含多个按钮,并调用多个startActivityForResult方法来打开不同的Activity处理业务,当这些新Activity关闭后,系统会调用前面的Activity的onActivityResult方法。为了知道该方法是由哪个请求结果所触发的,可利用requestCode标识,为了知道返回的数据来自于那个新的Activity,可利用resultCode标识
例,第二个Activity让用户选择信息然后把结果返回给第一个Activity
MainActivity.java
~~~
package com.hust.activityforresult;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
Button bn;
EditText edit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bn=(Button) findViewById(R.id.button1);
edit=(EditText) findViewById(R.id.editText1);
bn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//创建需要对应目标Activity的intent
Intent intent=new Intent(MainActivity.this,CityActivity.class);
//启动指定Activity并等待返回的结果,0是请求码。用于表示该请求
startActivityForResult(intent ,0);
}
});
}
//重写该方法,以回调的形式来获取Activity返回的结果
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
//当请求码和结果码都是0时,也就是处理的特定结果
if(requestCode==0&&resultCode==0){
//获取Bundle中的数据
Bundle bundle=data.getExtras();
String city=bundle.getString("city");
//修改编辑框的内容
edit.setText(city);
}
}
~~~
CityActivity.java
~~~
package com.hust.activityforresult;
import android.app.ExpandableListActivity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.TextView;
public class CityActivity extends ExpandableListActivity {
//定义省份
private String[] provinces={
"广东","湖北","河南"
};
//定义城市
private String[][] cities={
{"广州","深圳","珠海","惠州"},
{"武汉","襄阳","随州","十堰"},
{"郑州","平顶山","信阳","驻马店"}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_city);
ExpandableListAdapter ad=new BaseExpandableListAdapter(){
private TextView getTextView(){
AbsListView.LayoutParams lp=new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,64);
TextView textview =new TextView(CityActivity.this);
textview.setLayoutParams(lp);
textview.setTextSize(16);
textview.setGravity(Gravity.CENTER_VERTICAL|Gravity.LEFT);
textview.setPadding(30, 5, 0, 5);
return textview;
}
@Override
public int getGroupCount() {
// TODO Auto-generated method stub
return provinces.length;
}
@Override
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
return cities[groupPosition].length;
}
@Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return provinces[groupPosition];
}
@Override
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return cities[groupPosition][childPosition];
}
@Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return childPosition;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
TextView text=getTextView();
text.setTextColor(Color.RED);
text.setText(getGroup(groupPosition).toString());
return text;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
TextView text=getTextView();
text.setText(getChild(groupPosition, childPosition)
.toString());
return text;
}
//主要在给子选项设置监听器时,这个函数一定要返回true,否则监听器没有用
@Override
public boolean isChildSelectable(int groupPosition,
int childPosition) {
// TODO Auto-generated method stub
return true;
}
};
//设置adapter
setListAdapter(ad);
//为每个子选项添加监听器
getExpandableListView().setOnChildClickListener(new OnChildClickListener(){
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stub
//获取启动该Activity之前的Activity对应的Intent
Intent intent=getIntent();
intent.putExtra("city", cities[groupPosition][childPosition]);
//设置该Activity的结果码,并设置结束之后退回的Activity
CityActivity.this.setResult(0, intent);
//结束本Activity
CityActivity.this.finish();
return false;
}
});
}
~~~
![](https://box.kancloud.cn/2016-03-10_56e0d9b037ede.jpg)
![](https://box.kancloud.cn/2016-03-10_56e0d9b04d634.jpg)
![](https://box.kancloud.cn/2016-03-10_56e0d9b06135b.jpg)
- 前言
- Eclipse搭建android环境及Genymotion模拟器安装问题解决方法
- 表格布局(TableLayout)及重要属性
- 帧布局(FrameLayout)及属性
- layout_width和width,layout_height和height
- UI组件之TextView及其子类
- UI组件之TextView及其子类(一)TextView和EditText
- UI组件之TextView及其子类(二)RadioButton和CheckBox
- UI组件之TextView及其子类(三)ToggleButton和Switch
- UI组件之TextView及其子类(四)AnalogClock,DigitalClock
- UI组件之TextView及其子类(五)计时器Chronometer
- UI组件之ImageView及其子类(一)ImageView显示图片
- UI组件之ImageView及其子类(二)ImageButton ,ZoomButton
- UI组件之AdapterView及其子类关系,Adapter接口及其实现类关系
- UI组件之AdapterView及其子类(一)三种Adapter适配器填充ListView
- UI组件之AdapterView及其子类(二)GridView网格视图的使用
- UI组件之AdapterView及其子类(三)Spinner控件详解
- UI组件之AdapterView及其子类(四)Gallery画廊控件使用
- UI组件之AdapterView及其子类(五)ListView组件和ListActivity
- UI组件之AdapterView及其子类(六)ExpandableListView组件和ExpandableListActivity的使用
- UI组件之 ProgressBar及其子类(一)ProgressBar进度条的使用
- UI组件之ProgressBar及其子类(二)SeekBar拖动条和RatingBar星级评分条的使用
- ViewFlipper的功能和用法
- Toast的功能和用法
- TabHost选项卡的 功能和用法
- AlertDialog创建6种对话框的用法
- Android基于监听的事件处理机制
- Android基于回调的事件处理
- Handler消息传递机制(一)
- Handler消息传递机制(二)Handler,Loop,Message,MessageQueue的工作原理
- 启动Activity的两种方式startActivity和startActivityForResult(一)
- 启动Activity的两种方式startActivity和startActivityForResult(二)
- Activity的生命周期理解
- Bundle在Activity之间交换数据
- 通过 Intent 传递类对象
- Intent对象详解(一)
- Intent对象详解(二)
- 使用指定的Action,Category调用系统Activity
- 使用Action,Data属性启动系统Activity
- Android数据存储的三种方式-SharedPrefrences,File,SQLite