Android中返回数据到前一个Activity — startActivityForResult方法
本文讲述了Android中返回数据到前一个Activity — startActivityForResult方法。
实现步骤:
第一步:建立Android 工程:ActivityDemo。
第二步:编写Activity 的子类别:ActivityDemo,其程序代码如下:
package com.a3gs.activity; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.RadioGroup; public class ActivityDemo extends Activity { private RadioGroup rg; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button btn = (Button) findViewById(R.id.btn); btn.setOnClickListener(new Button.OnClickListener(){ @Override 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); } }); } @Override 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); }else rg.check(R.id.F); break; default: break; } } }
第三步:新建一个Activity 的子类别:Bundle2,其程序代码如下:
package com.a3gs.activity; import java.text.DecimalFormat; import java.text.NumberFormat; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class Bundle2 extends Activity { /** Called when the activity is first created. */ @Override 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 = "男性"; }else 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(){ @Override 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); return s; } /* 计算标准体重 */ private String getWeight(String sex,double height) { String weight=""; if(sex.equals("M")) { weight=format((height-80)*0.7); } else { weight=format((height-70)*0.6); } return weight; } }
第四步:修改res/layout/main.xml,其代码如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center_horizontal" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/title" android:gravity="center_horizontal" android:textSize="25px" /> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:gravity="center_vertical|center_horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" > <TextView 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="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:gravity="center_vertical|center_horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/myText1" android:gravity="center_horizontal" android:textSize="20px" /> <EditText android:id="@+id/height" android:layout_width="80px" android:layout_height="wrap_content" android:text="" android:numeric="decimal" android:textSize="20px" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/myText2" android:gravity="center_horizontal" android:textSize="20px" /> </LinearLayout> <Button 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="http://schemas.android.com/apk/res/android" > <TextView 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> <Button 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="http://schemas.android.com/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>
出处:http://ebaina.com/html/hottech/mobile/201001/25-875.html