/* (程序头部注释开始)
* 程序的版权和版本声明部分
* Copyright (c) , 烟台大学计算机学院学生
* All rights reserved.
* 文件名称:修改表达用户界面
* 作 者: 雷恒鑫
* 完成日期: 年 08 月7 日
* 版 本 号: V1.0
* 对任务及求解方法的描述部分
* 输入描述:
* 问题描述:
* 程序输出:
* 程序头部的注释结束
*/
以下是完整的BMI程序:
Bmi.java
package com.demo.android.bmi;
import java.text.DecimalFormat;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Bmi extends Activity {
/**
* Called when the activity is first created.
*
* @param
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Listen for button clicks
Button button = (Button) findViewById(R.id.submit);
button.setOnClickListener(calcBMI);
}
private OnClickListener calcBMI = new OnClickListener() {
public void onClick(View v) {
DecimalFormat nf = new DecimalFormat("0.00");
EditText fieldheight = (EditText) findViewById(R.id.height);
EditText fieldweight = (EditText) findViewById(R.id.weight);
double height = Double
.parseDouble(fieldheight.getText().toString()) / 100;
double weight = Double
.parseDouble(fieldweight.getText().toString());
double BMI = weight / (height * height);
TextView result = (TextView) findViewById(R.id.result);
result.setText("Your BMI is " + nf.format(BMI));
// Give health advice
TextView fieldsuggest = (TextView) findViewById(R.id.suggest);
if (BMI > 25) {
fieldsuggest.setText(R.string.advice_heavy);
} else if (BMI < 20) {
fieldsuggest.setText(R.string.advice_light);
} else {
fieldsuggest.setText(R.string.advice_average);
}
}
};
}
main.xml
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/height"
/>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:numeric="integer"
android:text=""
/>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/weight"
/>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:numeric="integer"
android:text=""
/>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/bmi_btn"
/>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
/>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
/>
advice.xml
你该多吃点,身体是革命的本钱哈。
体型很棒哦,继续保持。
你该节食了,呵呵。
strings.xml
BIM
身高(cm)
体重(kg)
计算 BMI 值
您的 BIM 值是:
运行结果:
经验积累:
1.BMI应用程序的
2.我知道了运算的时候是如何调用系统函数的。
3.我学会了如何声明一个Button实体。