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" /> <TextView android: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" /> <TextView android: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" /> <TextView android: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" /> <Button android: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"> <RadioButton android:id="@+id/sex1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="男的" /> <RadioButton android:id="@+id/sex2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="女的" /> </RadioGroup> <EditText android: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. */ @Override public 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给Intent intent.putExtras(bundle); //调用Activity2 startActivity(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 stub super.onCreate(savedInstanceState); //加载activity2.xml setContentView(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对象,并添加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; }}