安卓bmi项目_Android开发--身高体重指数(BIM)计算--完成BMI程序

/* (程序头部注释开始)

* 程序的版权和版本声明部分

* Copyright (c) 2011, 烟台大学计算机学院学生

* All rights reserved.

* 文件名称:修改表达用户界面

* 作 者: 雷恒鑫

* 完成日期: 2012 年 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应用程序的算法和C++的算法差不多。

2.我知道了运算的时候是如何调用系统函数的。

3.我学会了如何声明一个Button实体。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值