启动其它Activity并返回结果

来源:互联网 发布:js 表格增加行 编辑:程序博客网 时间:2024/05/18 03:07

       当前Activity重写onActivityResult(int requestCode, int resultCode, Intent intent)方法,该方法以回调的方式来获取指定Activity返回的结果; 

       被启动的Activity需要调用setResult()方法设置处理结果。


       

package org.crazyit.app;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;public class ActivityForResult extends Activity{Button bn;EditText city;@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);// 获取界面上的组件bn = (Button) findViewById(R.id.bn);city = (EditText) findViewById(R.id.city);// 为按钮绑定事件监听器bn.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View source){// 创建需要对应于目标Activity的IntentIntent intent = new Intent(ActivityForResult.this,SelectCityActivity.class);// 启动指定Activity并等待返回的结果,其中0是请求码,用于标识该请求startActivityForResult(intent, 0);}});}// 重写该方法,该方法以回调的方式来获取指定Activity返回的结果@Overridepublic void onActivityResult(int requestCode, int resultCode, Intent intent){// 当requestCode、resultCode同时为0,也就是处理特定的结果if (requestCode == 0 && resultCode == 0){// 取出Intent里的Extras数据Bundle data = intent.getExtras();// 取出Bundle中的数据String resultCity = data.getString("city");// 修改city文本框的内容city.setText(resultCity);}}}


被启动的Activity——SelectCityActivity返回结果时,OnActivityResult方法交将被回调。



/** * */package org.crazyit.app;import android.app.ExpandableListActivity;import android.content.Intent;import android.os.Bundle;import android.view.Gravity;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.ImageView;import android.widget.LinearLayout;import android.widget.TextView;public class SelectCityActivity extends ExpandableListActivity{// 定义省份数组private String[] provinces = new String[]{ "广东", "广西", "湖南"};private String[][] cities = new String[][]{{ "广州", "深圳", "珠海", "中山" },{ "桂林", "柳州", "南宁", "北海" },{ "长沙", "岳阳" , "衡阳" , "株洲" }};public void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);ExpandableListAdapter adapter = new BaseExpandableListAdapter(){// 获取指定组位置、指定子列表项处的子列表项数据@Overridepublic Object getChild(int groupPosition, int childPosition){return cities[groupPosition][childPosition];}@Overridepublic long getChildId(int groupPosition, int childPosition){return childPosition;}@Overridepublic int getChildrenCount(int groupPosition){return cities[groupPosition].length;}private TextView getTextView(){AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 64);TextView textView = new TextView(SelectCityActivity.this);textView.setLayoutParams(lp);textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);textView.setPadding(36, 0, 0, 0);textView.setTextSize(20);return textView;}// 该方法决定每个子选项的外观@Overridepublic View getChildView(int groupPosition, int childPosition,boolean isLastChild, View convertView, ViewGroup parent){TextView textView = getTextView();textView.setText(getChild(groupPosition, childPosition).toString());return textView;}// 获取指定组位置处的组数据@Overridepublic Object getGroup(int groupPosition){return provinces[groupPosition];}@Overridepublic int getGroupCount(){return provinces.length;}@Overridepublic long getGroupId(int groupPosition){return groupPosition;}// 该方法决定每个组选项的外观@Overridepublic View getGroupView(int groupPosition, boolean isExpanded,View convertView, ViewGroup parent){LinearLayout ll = new LinearLayout(SelectCityActivity.this);ll.setOrientation(0);ImageView logo = new ImageView(SelectCityActivity.this);ll.addView(logo);TextView textView = getTextView();textView.setText(getGroup(groupPosition).toString());ll.addView(textView);return ll;}@Overridepublic boolean isChildSelectable(int groupPosition,int childPosition){return true;}@Overridepublic boolean hasStableIds(){return true;}};// 设置该窗口显示列表setListAdapter(adapter);getExpandableListView().setOnChildClickListener(new OnChildClickListener(){@Overridepublic boolean onChildClick(ExpandableListView parent,View source, int groupPosition, int childPosition,long id){// 获取启动该Activity之前的Activity对应的IntentIntent intent = getIntent();intent.putExtra("city",cities[groupPosition][childPosition]);// 设置该SelectActivity的结果码,并设置结束之后退回的ActivitySelectCityActivity.this.setResult(0, intent);// 结束SelectCityActivity。SelectCityActivity.this.finish();return false;}});}}




0 0
原创粉丝点击