ITeye博客

startActivityForResult

trygood · 2011-03-29 15:49 · Android嵌入式开发 · 移动开发 · Markdown

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

① 新建工程

② 修改main.xml布局,添加UI元素

复制代码

③ 新建一个mylayout.xml布局,添加UI元素

<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"

<Button android:id="@+id/button_back" android:layout_width="100px" android:layout_height="48px" android:text="回上一页" android:layout_x="110px" android:layout_y="180px"

④ 新建一个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

复制代码

⑥ 修改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; } } 复制代码

⑧ 结果

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。