startActivityForResult 方法
来源:互联网 发布:php is numeric绕过 编辑:程序博客网 时间:2024/05/24 05:56
本帖最后由 地狱怒兽 于 2009-6-2 20:36 编辑
startActivityForResult 方法--返回数据到前一个Activity
① 新建工程
② 修改main.xml布局,添加UI元素
- <?xml version="1.0" encoding="utf-8"?>
- <AbsoluteLayout
- android:id="@+id/widget0"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- xmlns:android="http://schemas.android.com/apk/res/android">
- <TextView
- android:id="@+id/showText"
- android:layout_width="wrap_content"
- android:layout_height="26px"
- android:text="计算你的标准体重!"
- android:textSize="25px"
- android:layout_x="65px"
- android:layout_y="21px">
- </TextView>
- <TextView
- android:id="@+id/text_Sex"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="性别:"
- android:layout_x="71px"
- android:layout_y="103px">
- </TextView>
- <TextView
- android:id="@+id/text_Height"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="身高:"
- android:layout_x="72px"
- android:layout_y="169px">
- </TextView>
- <RadioGroup
- android:id="@+id/radioGroup"
- android:layout_width="wrap_content"
- android:layout_height="37px"
- android:orientation="horizontal"
- android:layout_x="124px"
- android:layout_y="101px">
- <RadioButton
- android:id="@+id/Sex_Man"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="男">
- </RadioButton>
- <RadioButton
- android:id="@+id/Sex_Woman"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="女">
- </RadioButton>
- </RadioGroup>
- <EditText
- android:id="@+id/height_Edit"
- android:layout_width="123px"
- android:layout_height="wrap_content"
- android:text=""
- android:numeric="decimal"
- android:textSize="18sp"
- android:layout_x="124px"
- android:layout_y="160px">
- </EditText>
- <Button
- android:id="@+id/button_OK"
- android:layout_width="80px"
- android:layout_height="wrap_content"
- android:text="计算"
- android:layout_x="125px"
- android:layout_y="263px">
- </Button>
- </AbsoluteLayout>
复制代码
③ 新建一个mylayout.xml布局,添加UI元素
- <?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/text1"
- 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/button_back"
- android:layout_width="100px"
- android:layout_height="48px"
- android:text="回上一页"
- android:layout_x="110px"
- android:layout_y="180px"
- ></Button>
- </AbsoluteLayout>
复制代码
④ 新建一个SecondActivity.java的Activity子类
- package zyf.Ex11_UI_A;
- import android.app.Activity;
- import android.os.Bundle;
- public class BMIActivity extends Activity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- }
- }
复制代码
⑤ 在AndroidManifest.xml中添加SecondActivity这个Activity
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="zyf.Ex11_UI_A"
- android:versionCode="1"
- android:versionName="1.0">
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <activity android:name=".Ex11_UI_A"
- 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="BMIActivity"></activity>
- </application>
- <uses-sdk android:minSdkVersion="2" />
- </manifest>
复制代码
⑥ 修改mainActivity.java代码
- package zyf.Ex11_UI_A;
- import android.app.Activity;/* import 相关class */
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.RadioButton;
- import android.widget.Toast;
- public class Ex11_UI_A extends Activity {
- protected int my_requestCode = 1550;
- private EditText edit_height;
- private RadioButton radiobutton_Man, radiobutton_Woman;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- /* 载入main.xml Layout */
- setContentView(R.layout.main);
- /* 以findViewById()取得Button 对象,并添加onClickListener */
- Button ok = (Button) findViewById(R.id.button_OK);
- edit_height = (EditText) findViewById(R.id.height_Edit);
- radiobutton_Man = (RadioButton) findViewById(R.id.Sex_Man);
- radiobutton_Woman = (RadioButton) findViewById(R.id.Sex_Woman);
- ok.setOnClickListener(new Button.OnClickListener() {
- public void onClick(View v) {
- try {
- /* 取得输入的身高 */
- double height = Double.parseDouble(edit_height.getText()
- .toString());
- /* 取得选择的性别 */
- String sex = "";
- if (radiobutton_Man.isChecked()) {
- sex = "M";
- } else {
- sex = "F";
- }
- /* new 一个Intent 对象,并指定class */
- Intent intent = new Intent();
- intent.setClass(Ex11_UI_A.this, BMIActivity.class);
- /* new 一个Bundle对象,并将要传递的数据传入 */
- Bundle bundle = new Bundle();
- bundle.putDouble("height", height);
- bundle.putString("sex", sex);
- /* 将Bundle 对象assign 给Intent */
- intent.putExtras(bundle);
- /* 调用Activity EX03_10_1 */
- startActivityForResult(intent, my_requestCode);
- } catch (Exception e) {
- // TODO: handle exception
- Toast.makeText(Ex11_UI_A.this,
- R.string.errorString, Toast.LENGTH_LONG).show();
- }
- }
- });
- }
- @Override
- protected void onActivityResult(int requestCode, int resultCode,
- Intent data) {
- // TODO Auto-generated method stub
- super.onActivityResult(requestCode, resultCode, data);
- switch (resultCode) {
- case RESULT_OK:
- /* 取得来自Activity2 的数据,并显示于画面上 */
- Bundle bunde = data.getExtras();
- String sex = bunde.getString("sex");
- double height = bunde.getDouble("height");
- edit_height.setText("" + height);
- if (sex.equals("M")) {
- radiobutton_Man.setChecked(true);
- } else {
- radiobutton_Woman.setChecked(true);
- }
- break;
- default:
- break;
- }
- }
- }
复制代码
⑦ 修改SecondActivity.java代码- package zyf.Ex11_UI_A;
- /* import 相关class */
- import java.text.DecimalFormat;
- import java.text.NumberFormat;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.TextView;
- public class BMIActivity extends Activity {
- private Intent intent;
- private Bundle bunde;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- /* 加载main.xml Layout */
- setContentView(R.layout.mylayout);
- /* 取得Intent 中的Bundle 对象 */
- intent = this.getIntent();
- bunde = intent.getExtras();
- /* 取得Bundle 对象中的数据 */
- String sex = bunde.getString("sex");
- double height = bunde.getDouble("height");
- /* 判断性别 */
- String sexText = "";
- if (sex.equals("M")) {
- sexText = "男性";
- } else {
- sexText = "女性";
- }
- /* 取得标准体重 */
- String weight = this.getWeight(sex, height);
- /* 设置输出文字 */
- TextView tv1 = (TextView) findViewById(R.id.text1);
- tv1.setText("你是一位" + sexText + "\n你的身高是" + height +
- "厘米\n你的标准体重是"+ weight + "公斤");
- /* 以findViewById()取得Button 对象,并添加onClickListener */
- Button b1 = (Button) findViewById(R.id.button_back);
- b1.setOnClickListener(new Button.OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- /* 返回result 回上一个activity */
- BMIActivity.this.setResult(RESULT_OK, intent);
- /* 结束这个activity */
- BMIActivity.this.finish();
- }
- });
- }
- /* 四舍五入的method */
- private String format(double num) {
- NumberFormat formatter = new DecimalFormat("0.00");
- String s = formatter.format(num);
- return s;
- }
- /* 以findViewById()取得Button 对象,并添加onClickListener */
- 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;
- }
- }
复制代码
⑧ 结果