在上一篇里面写了Activity之间的传值,这一篇里面写写怎么启动一个新的Activity之后,得到它的返回值
获得返回值需要重写当前Activity的onActivityResult(int requestCode,int resultCode,Intent intent),requestCode代表请求码,而resultCode代表结果码
下面是一个例子,点击按钮,弹出城市选择框,选中的城市返回显示到文本框上
界面xml如下
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="@+id/bn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="选择您所在的城市" />
<EditText
android:id="@+id/city"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10" android:width="180dp">
<requestFocus />
</EditText>
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TableRow>
<TableRow
android:id="@+id/tableRow4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TableRow>
</TableLayout>
</LinearLayout>
代码如下,重点是onActivityResult方法
package WangLi.Activity.ActivityForResult;
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;
/** Called when the activity is first created. */
@Override
public 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(){
public void onClick(View source)
{
//创建需要对应于目标Activity的Intent
Intent intent = new Intent(ActivityForResult.this,SelectCityActivity.class);
//启动指定Activity并等待返回结果,其中0是请求码,用于标识该请求
startActivityForResult(intent,0);
}
});
}
@Override
public void onActivityResult(int requestCode,int resultCode,Intent intent)
{
//当requestCode,resultCode同时为0时,也就是处理特定的结果
if(requestCode == 0 && resultCode == 0)
{
//取出Intent里的数据
Bundle data = intent.getExtras();
//取出Bundle中的数据
String resultCity = data.getString("city");
//修改city文本框
city.setText(resultCity);
}
}
}
点击后出现选择城市的窗口,界面全部是动态生成,没有xml文件
选中后,此窗口关闭,回到第一个窗口,显示所选的城市
选择城市代码窗口如下
package WangLi.Activity.ActivityForResult;
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 saveInstanceState)
{
super.onCreate(saveInstanceState);
ExpandableListAdapter adapter = new BaseExpandableListAdapter()
{
//获取指定组位置,指定子列表项处的子列表项数据
public Object getChild(int groupPosition,int childPosition)
{
return cities[groupPosition][childPosition];
}
public long getChildId(int groupPosition,int childPosition)
{
return childPosition;
}
public int getChildrenCount(int groupPosition)
{
return cities[groupPosition].length;
}
private TextView getTextView()
{
AbsListView.LayoutParams lp =
new AbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_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;
}
public View getChildView(int groupPosition,int childPosition,boolean isLastChild,
View convertView,ViewGroup parent)
{
TextView textView = getTextView();
textView.setText(getChild(groupPosition,childPosition).toString());
return textView;
}
public Object getGroup(int groupPosition)
{
return provinces[groupPosition];
}
public int getGroupCount()
{
return provinces.length;
}
public long getGroupId(int groupPosition)
{
return groupPosition;
}
public View getGroupView(int groupPosition,boolean isExpanded,
View convertView,ViewGroup parent)
{
LinearLayout l1 = new LinearLayout(SelectCityActivity.this);
l1.setOrientation(0);
ImageView logo = new ImageView(SelectCityActivity.this);
l1.addView(logo);
TextView textView = getTextView();
textView.setText(getGroup(groupPosition).toString());
l1.addView(textView);
return l1;
}
public boolean isChildSelectable(int groupPosition,int childPosition)
{
return true;
}
public boolean hasStableIds()
{
return true;
}
};
//设置窗口显示列表
setListAdapter(adapter);
getExpandableListView().setOnChildClickListener(new OnChildClickListener()
{
public boolean onChildClick(ExpandableListView parent,View source,
int groupPosition,int childPosition,long id)
{
//获取启动该Activity之前的Activity对应的Intent
Intent intent = getIntent();
Bundle data = new Bundle();
data.putString("city", cities[groupPosition][childPosition]);
intent.putExtras(data);
//设置该SelectActivity的结果码,并设置结束之后退回的Activity
SelectCityActivity.this.setResult(0,intent);
//结束SelectActivity
SelectCityActivity.this.finish();
return false;
}
});
}
}