在普通的java 类中怎么获取startactivity 参数forresult

本文讲述了Android中返回数据到前一个Activity&&&startActivityForResult方法。
实现步骤:
第一步:建立Android&工程:ActivityDemo。
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
第二步:编写Activity&的子类别:ActivityDemo,其程序代码如下:
package com.a3gs.
import android.app.A
import android.content.I
import android.os.B
import android.view.V
import android.widget.B
import android.widget.EditT
import android.widget.RadioG
public class ActivityDemo extends Activity {
private RadioG
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
EditText et = (EditText) findViewById(R.id.height);
// 判断身高如果为空,则不提交
if(et.getText().toString().equals("")) return;
double height = Double.parseDouble(et.getText().toString());
String sex = "";
rg = (RadioGroup) findViewById(R.id.sex);
if(rg.getCheckedRadioButtonId() == R.id.M) {
sex = "M";
if(rg.getCheckedRadioButtonId() == R.id.F) {
sex = "F";
// 用Bundle来绑定所要的数据
Bundle bd = new Bundle();
bd.putDouble("height", height);
bd.putString("sex", sex);
// 创建一个新的Intent,并将Bundle传进去
Intent it = new Intent();
it.putExtras(bd);
it.setClass(ActivityDemo.this, Bundle2.class);
startActivityForResult(it, RESULT_OK);
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
switch (resultCode) {
case RESULT_OK:
String sex = data.getStringExtra("sex");
if(sex.equals("M")){
rg.check(R.id.M);
rg.check(R.id.F);
第三步:新建一个Activity&的子类别:Bundle2,其程序代码如下:
package com.a3gs.
import java.text.DecimalF
import java.text.NumberF
import android.app.A
import android.os.B
import android.view.V
import android.widget.B
import android.widget.TextV
public class Bundle2 extends Activity {
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myalyout);
Bundle bundle = getIntent().getExtras();
String sex = bundle.getString("sex");
Double height = bundle.getDouble("height");
String weight = this.getWeight(sex, height);
String sexStr = "";
if(sex.equals("M")){
sexStr = "男性";
sexStr = "女性";
String result = "您是一位" + sexStr +
"\n您的身高为:" + height + "cm" +
"\n您的标准体重为:" + weight +"千克";
TextView tv = (TextView) findViewById(R.id.tv);
tv.setText(result);
Button backBtn = (Button) findViewById(R.id.back);
backBtn.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
Bundle2.this.setResult(RESULT_OK, getIntent());
Bundle2.this.finish();
/* 四舍五入的method */
private String format(double num)
NumberFormat formatter = new DecimalFormat("0.00");
String s=formatter.format(num);
/* 计算标准体重 */
private String getWeight(String sex,double height)
String weight="";
if(sex.equals("M"))
weight=format((height-80)*0.7);
weight=format((height-70)*0.6);
第四步:修改res/layout/main.xml,其代码如下:
&?xml version="1.0" encoding="utf-8"?&
&LinearLayout xmlns:android="/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/title"
android:gravity="center_horizontal"
android:textSize="25px"
&LinearLayout xmlns:android="/apk/res/android"
android:orientation="horizontal"
android:gravity="center_vertical|center_horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/myText"
android:gravity="center_horizontal"
android:textSize="20px"
&RadioGroup
android:id="@+id/sex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_horizontal"
android:textSize="20px"
&RadioButton
android:id="@+id/M"
android:checked="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男"
&/RadioButton&
&RadioButton
android:id="@+id/F"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女"
&/RadioButton&
&/RadioGroup&
&/LinearLayout&
&LinearLayout xmlns:android="/apk/res/android"
android:orientation="horizontal"
android:gravity="center_vertical|center_horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/myText1"
android:gravity="center_horizontal"
android:textSize="20px"
android:id="@+id/height"
android:layout_width="80px"
android:layout_height="wrap_content"
android:text=""
android:numeric="decimal"
android:textSize="20px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/myText2"
android:gravity="center_horizontal"
android:textSize="20px"
&/LinearLayout&
android:id="@+id/btn"
android:layout_width="130px"
android:layout_height="wrap_content"
android:text="@string/btnText"
android:gravity="center_horizontal|center_vertical"
android:textSize="20px"
&/LinearLayout&
第五步:修改res/layout/mylayout.xml,其代码如下:
&?xml version="1.0" encoding="utf-8"?&
&AbsoluteLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="/apk/res/android"
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_x="50px"
android:layout_y="72px"
&/TextView&
android:id="@+id/back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_x="80px"
android:layout_y="152px"
android:text="@string/backText"
&/AbsoluteLayout&
第六步:修改AndroidManifest.xml,其内容如下:
&?xml version="1.0" encoding="utf-8"?&
&manifest xmlns:android="/apk/res/android"
package="com.a3gs.activity"
android:versionCode="1"
android:versionName="1.0"&
&application android:icon="@drawable/icon" android:label="@string/app_name"&
&activity android:name=".ActivityDemo"
android:label="@string/app_name"&
&intent-filter&
&action android:name="android.intent.action.MAIN" /&
&category android:name="android.intent.category.LAUNCHER" /&
&/intent-filter&
&/activity&
&activity android:name=".Bundle2"/&
&/application&
&uses-sdk android:minSdkVersion="3" /&
&/manifest&
阅读(...) 评论()启动Activity的两种方法 startActivityForResult与 startActivity
启动Activity的两种方法 startActivityForResult与 startActivity
发布时间: 23:42:04
编辑:www.fx114.net
本篇文章主要介绍了"启动Activity的两种方法 startActivityForResult与 startActivity",主要涉及到启动Activity的两种方法 startActivityForResult与 startActivity方面的内容,对于启动Activity的两种方法 startActivityForResult与 startActivity感兴趣的同学可以参考一下。
第一种方法请看http://blog.csdn.net/x/article/details/9902895
欢迎加入QQ交流3群:
Intent intent=new Intent();
intent.setClass(MainActivity.this, SecondActivity.class);
MainActivity.this.startActivity(intent);而另一种是打开一个特定的Activity取得数据之后,返回给主界面的Activity;因为可能在复杂的主界面中,如果再来操作一些数据,可能显得更烦琐。
& 操作的基本步骤:1.主界面调用startActivityForResult(Intent intent,int requestCode)启动特定的Activity;其中第二个参数requestCode表示特定的请求码
& & & & & & & & & & & & & & & &2.在特定的Activity中得到数据后将数据放入主界面对应的Intent中
& & & & & & & & & & & & & & & &3.设置当前Activity的结果码
& & & & & & & & & & & & & & & &5.关闭当前的Activity,返回主界面;在主界面会触发(int requestCode, int resultCode, Intent data)方法;参数分别在 请求码,结果码,取得的数据
& & & & & & & & & & & & & & & &6.主界面根据请求码和结果码来判断是否在特定的Activity取得的数据
MainActivity.java
package com.example.starta
import android.app.A
import android.content.I
import android.os.B
import android.view.M
import android.view.V
import android.view.View.OnClickL
import android.widget.B
import android.widget.EditT
public class MainActivity extends Activity {
Button bt1;
EditText ed1;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt1 = (Button) findViewById(R.id.bt1);
ed1 = (EditText) findViewById(R.id.ed1);
bt1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent(MainActivity.this,CitysActivity.class);
//启动CitysActivity 请求码为0
startActivityForResult(intent, 0);
// 回调的方式来获取指定Activity返回的结果
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==0&&resultCode==0){
Bundle bundle=data.getBundleExtra(&name&);
ed1.setText(bundle.getString(&name&)+&--&+bundle.getString(&names&));
public boolean onCreateOptionsMenu(Menu menu) {
// I this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
CitysActivity.java
package com.example.starta
import java.util.ArrayL
import java.util.HashM
import java.util.L
import java.util.M
import android.app.A
import android.content.I
import android.os.B
import android.view.V
import android.view.View.OnClickL
import android.widget.ExpandableListV
import android.widget.ExpandableListView.OnChildClickL
import android.widget.SimpleExpandableListA
import android.widget.TextV
public class CitysActivity extends Activity {
ExpandableListView exp1;
String[] city = { &上海&, &湖南& };
String[][] citys = { { &普陀区&, &浦东新区&, &长宁区& }, { &益阳市&, &衡阳市&, &岳阳市& } };
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_city);
exp1 = (ExpandableListView) findViewById(R.id.exp1);
// 定义父列表项
List&Map&String, String&& parentlist = new ArrayList&Map&String, String&&();
// 定义子列表项
List&List&Map&String, String&&& chlilist = new ArrayList&List&Map&String, String&&&();
for (int i = 0; i & city. i++) {
// 添加父列表向数据
Map&String, String& map = new HashMap&String, String&();
map.put(&pnames&, city[i]);
parentlist.add(map);
List&Map&String, String&& parentlists = new ArrayList&Map&String, String&&();
for (int j = 0; j & citys[i]. j++) {
// 添加子列表项
Map&String, String& maps = new HashMap&String, String&();
maps.put(&names&, citys[i][j]);
parentlists.add(maps);
chlilist.add(parentlists);
final SimpleExpandableListAdapter simple = new SimpleExpandableListAdapter(
this, parentlist, R.layout.parantcity,
new String[] { &pnames& }, new int[] { R.id.pa1 }, chlilist,
R.layout.chicity, new String[] { &names& },
new int[] { R.id.pa2 });
exp1.setAdapter(simple);
exp1.setOnChildClickListener(new OnChildClickListener() {
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
//获取选中的值
String cityO=simple.getGroup(groupPosition).toString();
String cityP=simple.getChild(groupPosition, childPosition).toString();
String []city=cityO.substring(1, cityO.length()-1).split(&=&);
String []citys=cityP.substring(1, cityO.length()-1).split(&=&);
//将数据放入Bundle对象中
Bundle bundle=new Bundle();
bundle.putString(&name&, city[1]);
bundle.putString(&names&, citys[1]);
// 获取启动该Activity之前的Activity对应的Intent
Intent intent=CitysActivity.this.getIntent();
intent.putExtra(&name&, bundle);
//设置当前Activity的结果码
CitysActivity.this.setResult(0, intent);
//关闭当前的Activity
CitysActivity.this.finish();
activity_city.xml
&?xml version=&1.0& encoding=&utf-8&?&
&LinearLayout xmlns:android=&/apk/res/android&
android:layout_width=&match_parent&
android:layout_height=&match_parent&
android:orientation=&vertical& &
&ExpandableListView
android:id=&@+id/exp1&
android:layout_width=&match_parent&
android:layout_height=&wrap_content& &
&/ExpandableListView&
&/LinearLayout&
activity_main.xml
&RelativeLayout xmlns:android=&/apk/res/android&
xmlns:tools=&/tools&
android:layout_width=&match_parent&
android:layout_height=&match_parent&
tools:context=&.MainActivity& &
android:id=&@+id/bt1&
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:text=&@string/city&/&
android:id=&@+id/ed1&
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:layout_toRightOf=&@id/bt1&
android:layout_alignParentRight=&true&/&
&/RelativeLayout&
& &chicity.xml
&?xml version=&1.0& encoding=&utf-8&?&
&TextView xmlns:android=&/apk/res/android&
android:id=&@+id/pa2&
android:layout_width=&match_parent&
android:layout_height=&match_parent& &
&/TextView&
parantcity.xml
&?xml version=&1.0& encoding=&utf-8&?&
&TextView xmlns:android=&/apk/res/android&
android:id=&@+id/pa1&
android:layout_width=&match_parent&
android:layout_height=&match_parent& &
&/TextView&
一、不得利用本站危害国家安全、泄露国家秘密,不得侵犯国家社会集体的和公民的合法权益,不得利用本站制作、复制和传播不法有害信息!
二、互相尊重,对自己的言论和行为负责。
本文标题:
本页链接:2014年6月 移动开发大版内专家分月排行榜第一2014年3月 移动开发大版内专家分月排行榜第一
2014年11月 移动开发大版内专家分月排行榜第二2014年4月 移动开发大版内专家分月排行榜第二2014年2月 移动开发大版内专家分月排行榜第二
2015年2月 移动开发大版内专家分月排行榜第二
2015年4月 移动开发大版内专家分月排行榜第三2015年1月 移动开发大版内专家分月排行榜第三
匿名用户不能发表回复!|
每天回帖即可获得10分可用分!小技巧:
你还可以输入10000个字符
(Ctrl+Enter)
请遵守CSDN,不得违反国家法律法规。
转载文章请注明出自“CSDN(www.csdn.net)”。如是商业用途请联系原作者。}

我要回帖

更多关于 startactivity 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信