startActivityForResult 方法

来源:互联网 发布:php is numeric绕过 编辑:程序博客网 时间:2024/05/24 05:56
本帖最后由 地狱怒兽 于 2009-6-2 20:36 编辑

startActivityForResult 方法--返回数据到前一个Activity

① 新建工程

② 修改main.xml布局,添加UI元素
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <AbsoluteLayout 
  3.     android:id="@+id/widget0"
  4. android:layout_width="fill_parent" 
  5. android:layout_height="fill_parent"
  6. xmlns:android="http://schemas.android.com/apk/res/android">
  7. <TextView 
  8.     android:id="@+id/showText"
  9.     android:layout_width="wrap_content"
  10.     android:layout_height="26px"
  11.     android:text="计算你的标准体重!"
  12.     android:textSize="25px"
  13.     android:layout_x="65px"
  14.     android:layout_y="21px">
  15. </TextView>
  16. <TextView 
  17.     android:id="@+id/text_Sex" 
  18.     android:layout_width="wrap_content"
  19. android:layout_height="wrap_content" 
  20. android:text="性别:"
  21. android:layout_x="71px"
  22.         android:layout_y="103px">
  23. </TextView>
  24. <TextView 
  25.     android:id="@+id/text_Height" 
  26.     android:layout_width="wrap_content"
  27. android:layout_height="wrap_content" 
  28. android:text="身高:"
  29.         android:layout_x="72px"
  30.         android:layout_y="169px">
  31. </TextView>
  32. <RadioGroup 
  33.     android:id="@+id/radioGroup" 
  34.     android:layout_width="wrap_content"
  35. android:layout_height="37px" 
  36. android:orientation="horizontal"
  37. android:layout_x="124px" 
  38. android:layout_y="101px">
  39. <RadioButton 
  40.     android:id="@+id/Sex_Man"
  41. android:layout_width="wrap_content" 
  42. android:layout_height="wrap_content"
  43. android:text="男">
  44. </RadioButton>
  45. <RadioButton 
  46.     android:id="@+id/Sex_Woman"
  47. android:layout_width="wrap_content" 
  48. android:layout_height="wrap_content"
  49. android:text="女">
  50. </RadioButton>
  51. </RadioGroup>
  52. <EditText 
  53.     android:id="@+id/height_Edit" 
  54.     android:layout_width="123px"
  55. android:layout_height="wrap_content" 
  56. android:text=""
  57. android:numeric="decimal"
  58. android:textSize="18sp" 
  59. android:layout_x="124px" 
  60. android:layout_y="160px">
  61. </EditText>
  62. <Button 
  63.     android:id="@+id/button_OK" 
  64.     android:layout_width="80px"
  65. android:layout_height="wrap_content" 
  66. android:text="计算"
  67. android:layout_x="125px" 
  68. android:layout_y="263px">
  69. </Button>
  70. </AbsoluteLayout>
复制代码

③ 新建一个mylayout.xml布局,添加UI元素
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <AbsoluteLayout
  3.     android:layout_width="fill_parent"
  4.     android:layout_height="fill_parent"
  5.     xmlns:android="http://schemas.android.com/apk/res/android"
  6. >
  7. <TextView
  8.     android:id="@+id/text1"
  9.     android:layout_width="wrap_content"
  10.     android:layout_height="wrap_content"
  11.     android:textSize="20sp"
  12.     android:layout_x="50px"
  13.     android:layout_y="72px"
  14. ></TextView>
  15. <Button
  16.     android:id="@+id/button_back"
  17.     android:layout_width="100px"
  18.     android:layout_height="48px"
  19.     android:text="回上一页"
  20.     android:layout_x="110px"
  21.     android:layout_y="180px"
  22. ></Button>
  23. </AbsoluteLayout>
复制代码

④ 新建一个SecondActivity.javaActivity子类
  1. package zyf.Ex11_UI_A;

  2. import android.app.Activity;
  3. import android.os.Bundle;

  4. public class BMIActivity extends Activity {
  5. /** Called when the activity is first created. */
  6. @Override
  7. public void onCreate(Bundle savedInstanceState) {
  8. super.onCreate(savedInstanceState);
  9. }
  10. }
复制代码

⑤ AndroidManifest.xml中添加SecondActivity这个Activity
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3.       package="zyf.Ex11_UI_A"
  4.       android:versionCode="1"
  5.       android:versionName="1.0">
  6.     <application android:icon="@drawable/icon" android:label="@string/app_name">
  7.         <activity android:name=".Ex11_UI_A"
  8.                   android:label="@string/app_name">
  9.             <intent-filter>
  10.                 <action android:name="android.intent.action.MAIN" />
  11.                 <category android:name="android.intent.category.LAUNCHER" />
  12.             </intent-filter>
  13.         </activity>
  14.     <activity android:name="BMIActivity"></activity>
  15. </application>
  16.     <uses-sdk android:minSdkVersion="2" />
  17. </manifest>
复制代码

⑥ 修改mainActivity.java代码
  1. package zyf.Ex11_UI_A;
  2. import android.app.Activity;/* import 相关class */
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.widget.Button;
  7. import android.widget.EditText;
  8. import android.widget.RadioButton;
  9. import android.widget.Toast;
  10. public class Ex11_UI_A extends Activity {
  11. protected int my_requestCode = 1550;
  12. private EditText edit_height;
  13. private RadioButton radiobutton_Man, radiobutton_Woman;
  14. /** Called when the activity is first created. */
  15. @Override
  16. public void onCreate(Bundle savedInstanceState) {
  17. super.onCreate(savedInstanceState);
  18. /* 载入main.xml Layout */
  19. setContentView(R.layout.main);
  20. /* 以findViewById()取得Button 对象,并添加onClickListener */
  21. Button ok = (Button) findViewById(R.id.button_OK);
  22. edit_height = (EditText) findViewById(R.id.height_Edit);
  23. radiobutton_Man = (RadioButton) findViewById(R.id.Sex_Man);
  24. radiobutton_Woman = (RadioButton) findViewById(R.id.Sex_Woman);
  25. ok.setOnClickListener(new Button.OnClickListener() {
  26. public void onClick(View v) {
  27. try {
  28. /* 取得输入的身高 */
  29. double height = Double.parseDouble(edit_height.getText()
  30. .toString());
  31. /* 取得选择的性别 */
  32. String sex = "";
  33. if (radiobutton_Man.isChecked()) {
  34. sex = "M";
  35. } else {
  36. sex = "F";
  37. }
  38. /* new 一个Intent 对象,并指定class */
  39. Intent intent = new Intent();
  40. intent.setClass(Ex11_UI_A.this, BMIActivity.class);
  41. /* new 一个Bundle对象,并将要传递的数据传入 */
  42. Bundle bundle = new Bundle();
  43. bundle.putDouble("height", height);
  44. bundle.putString("sex", sex);
  45. /* 将Bundle 对象assign 给Intent */
  46. intent.putExtras(bundle);
  47. /* 调用Activity EX03_10_1 */
  48. startActivityForResult(intent, my_requestCode);
  49. } catch (Exception e) {
  50. // TODO: handle exception
  51. Toast.makeText(Ex11_UI_A.this, 
  52.                                R.string.errorString, Toast.LENGTH_LONG).show();
  53. }
  54. }
  55. });
  56. }
  57. @Override
  58. protected void onActivityResult(int requestCode, int resultCode, 
  59.                                                                     Intent data) {
  60. // TODO Auto-generated method stub
  61. super.onActivityResult(requestCode, resultCode, data);
  62. switch (resultCode) {
  63. case RESULT_OK:
  64. /* 取得来自Activity2 的数据,并显示于画面上 */
  65. Bundle bunde = data.getExtras();
  66. String sex = bunde.getString("sex");
  67. double height = bunde.getDouble("height");
  68. edit_height.setText("" + height);
  69. if (sex.equals("M")) {
  70. radiobutton_Man.setChecked(true);
  71. } else {
  72. radiobutton_Woman.setChecked(true);
  73. }
  74. break;
  75. default:
  76. break;
  77. }
  78. }
  79. }
复制代码

⑦ 修改SecondActivity.java代码
  1. package zyf.Ex11_UI_A;
  2. /* import 相关class */
  3. import java.text.DecimalFormat;
  4. import java.text.NumberFormat;
  5. import android.app.Activity;
  6. import android.content.Intent;
  7. import android.os.Bundle;
  8. import android.view.View;
  9. import android.widget.Button;
  10. import android.widget.TextView;

  11. public class BMIActivity extends Activity {
  12. private Intent intent;
  13. private Bundle bunde;
  14. /** Called when the activity is first created. */
  15. @Override
  16. public void onCreate(Bundle savedInstanceState) {
  17. super.onCreate(savedInstanceState);
  18. /* 加载main.xml Layout */
  19. setContentView(R.layout.mylayout);

  20. /* 取得Intent 中的Bundle 对象 */
  21. intent = this.getIntent();
  22. bunde = intent.getExtras();

  23. /* 取得Bundle 对象中的数据 */
  24. String sex = bunde.getString("sex");
  25. double height = bunde.getDouble("height");
  26. /* 判断性别 */
  27. String sexText = "";
  28. if (sex.equals("M")) {
  29. sexText = "男性";
  30. } else {
  31. sexText = "女性";
  32. }
  33. /* 取得标准体重 */
  34. String weight = this.getWeight(sex, height);
  35. /* 设置输出文字 */
  36. TextView tv1 = (TextView) findViewById(R.id.text1);
  37. tv1.setText("你是一位" + sexText + "\n你的身高是" + height +
  38.                                           "厘米\n你的标准体重是"+ weight + "公斤");


  39. /* 以findViewById()取得Button 对象,并添加onClickListener */
  40. Button b1 = (Button) findViewById(R.id.button_back);
  41. b1.setOnClickListener(new Button.OnClickListener() {

  42. @Override
  43. public void onClick(View v) {
  44. // TODO Auto-generated method stub
  45. /* 返回result 回上一个activity */
  46. BMIActivity.this.setResult(RESULT_OK, intent);
  47. /* 结束这个activity */
  48. BMIActivity.this.finish();
  49. }
  50. });
  51. }
  52. /* 四舍五入的method */
  53. private String format(double num) {
  54. NumberFormat formatter = new DecimalFormat("0.00");
  55. String s = formatter.format(num);
  56. return s;
  57. }
  58. /* 以findViewById()取得Button 对象,并添加onClickListener */
  59. private String getWeight(String sex, double height) {
  60. String weight = "";
  61. if (sex.equals("M")) {
  62. weight = format((height - 80) * 0.7);
  63. } else {
  64. weight = format((height - 70) * 0.6);
  65. }
  66. return weight;
  67. }
  68. }
复制代码

⑧ 结果

图片17.png