activity1.xml
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/widget0"android:layout_width="fill_parent"android:layout_height="fill_parent" ><TextView
android:id="@+id/title"android:layout_width="243px"android:layout_height="29px"android:text="@string/title"android:textSize="24sp"android:layout_x="36px"android:layout_y="32px" /><TextViewandroid:id="@+id/text1"android:layout_width="wrap_content"android:layout_height="37px"android:text="@string/text1"android:textSize="18sp"android:layout_x="40px"android:layout_y="156px" /><TextViewandroid:id="@+id/text2"android:layout_width="wrap_content"android:layout_height="29px"android:text="@string/text2"android:textSize="18sp"android:layout_x="40px"android:layout_y="102px" /><TextViewandroid:id="@+id/text3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="cm"android:textSize="18sp"android:layout_x="231px"android:layout_y="157px" /><Buttonandroid:id="@+id/button1"android:layout_width="70px"android:layout_height="48px"android:layout_x="130px"android:layout_y="232px"android:text="计算" /><RadioGroup android:id="@+id/sex"android:layout_width="300px"android:layout_height="100px"xmlns:android="http://schemax.android.com/apk/res/android"android:layout_x="97px"android:layout_y="98px"android:orientation="horizontal"android:checkedButton="@+id/sex1"><RadioButtonandroid:id="@+id/sex1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="男的" /><RadioButtonandroid:id="@+id/sex2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="女的" /></RadioGroup><EditTextandroid:id="@+id/height"android:layout_width="130px"android:layout_height="wrap_content"android:textSize="18sp"android:layout_x="96px"android:layout_y="142px"android:numeric="decimal"></EditText>"</AbsoluteLayout>
activity2.xml
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent" ><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" /></AbsoluteLayout>
Activity1.java
package com.bund;
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.RadioButton;public class Activity1 extends Activity {
/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);setContentView(R.layout.activity1);Button b1 = (Button) findViewById(R.id.button1);b1.setOnClickListener(new Button.OnClickListener(){@Override
public void onClick(View v) { //取得输入的身高EditText et = (EditText) findViewById(R.id.height);double height = Double.parseDouble(et.getText().toString());//取得选择的性别String sex = "";RadioButton rb1 = (RadioButton) findViewById(R.id.sex1);if(rb1.isChecked()){ sex = "M";}else{ sex = "F";}Intent intent = new Intent();intent.setClass(Activity1.this, Activity2.class);//new 一个Bundle对象,并将要传递的数据传入Bundle bundle = new Bundle();bundle.putDouble("height", height);bundle.putString("sex", sex);//将Bundle对象assign给Intentintent.putExtras(bundle);//调用Activity2startActivity(intent);}});}}
activity2.java
package com.bund;
import java.text.DecimalFormat;
import java.text.NumberFormat;import android.app.Activity;import android.os.Bundle;import android.widget.TextView;public class Activity2 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stubsuper.onCreate(savedInstanceState);//加载activity2.xmlsetContentView(R.layout.activity2);//得Intent中的Bundle对象Bundle bunde = this.getIntent().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+"公斤");}//四舍五入private String format(double num){ NumberFormat formatter = new DecimalFormat("0.00");String s = formatter.format(num);return s;}//以findViewById()取得Button对象,并添加onClickListenerprivate 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;}}