您的位置:首页 > 其它

【BMI指数计算器V1.0】项目实战

2015-07-25 00:18 495 查看

项目背景

BMI指数(即身体质量指数,简称体质指数又称体重,英文为Body
Mass Index,简称BMI),是用体重公斤数除以身高米数平方得出的数字,是目前国际上常用的衡量人体胖瘦程度以及是否健康的一个标准。主要用于统计用途,当我们需要比较及分析一个人的体重对于不同高度的人所带来的健康影响时,BMI值是一个中立而可靠的指标

BMI值原来的设计是一个用于公众健康研究的统计工具。当我们需要知道肥胖是否为某一疾病的致病原因时,我们可以把病人的身高及体重换算成BMI值,再找出其数值及病发率是否有线性关连。不过,随着科技进步,现时BMI值只是一个参考值。要真正量度病人是否肥胖,体脂肪率比BMI更准确、而腰围身高比又比体脂肪率好、但是最好的看法是看内脏脂肪(若内脏脂肪正常,就算腰围很大及体脂肪率很高,健康风险不高,日本相扑很多都是这种胖法)。因此,BMI的角色也慢慢改变,从医学上的用途,变为一般大众的纤体指标。BMI是Body
Mass Index 的缩写,BMI中文是“体质指数”的意思,是以你的身高体重计算出来的。BMI是世界公认的一种评定肥胖程度的分级方法,世界卫生组织(WHO)也以BMI来对肥胖或超重进行定义。

体质指数算法:

体质指数(BMI)=体重(kg)÷身高^2(m)

EX:70kg÷(1.75×1.75)=22.8

指标:

WHO标准
亚洲标准
中国标准
相关疾病发病危险性
偏瘦
<18.5

低(但其它疾病危险性增加)

正常
18.5-24.9

18.5-22.9

18.5-23.9

平均水平

超重
≥25

≥23

≥24

 

偏胖
25.0~29.9

23~24.9
24~27.9

增加

肥胖
30.0~34.9

25~29.9

≥28

中度增加

重度肥胖
35.0~39.9

≥30

——

严重增加

极重度肥胖
≥40.0

非常严重增加

不适合人群:

并不是每个人都适用BMI的,如:

1.未满18岁;

2.是运动员;

3.正在做重量训练;

4.怀孕或哺乳中;

5.身体虚弱或久坐不动的老人。

如果认为BMI算出来的结果不能正确反映体重问题,请带着结果与医师讨论,并要求做体脂肪测试。

项目效果

静态效果图:





动态效果图:



项目结构



注意:此项目是在依赖appcompat_v7_9项目下创建的,需要导入appcompat_v7_9并依赖此项目。
<uses-sdk

android:minSdkVersion="14"

android:targetSdkVersion="19" />


界面开发

注册文件:AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.kedi.bmi"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.kedi.bmi.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
</span>
字符串资源文件:values/string.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

<!-- 字符串资源文件 -->
<string name="app_name">BMI指数计算器</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="title">BMI指数计算器</string>
<string name="height">您的身高</string>
<string name="cm">(厘米)cm</string>
<string name="weight">您的体重</string>
<string name="kg">(千克)kg</string>
<string name="cala">计算</string>
<string name="re_cala">重新计算</string>
<string name="weight_bmi">您的体重指数:</string>
<string name="weight_state">您的体重状况:</string>
<string name="tip1">身高不能为空</string>
<string name="tip2">输入格式不正确</string>
<string name="tip3">体重不能为空</string>
</resources>
尺寸资源文件:values/dimens.xml
<resources>

<!-- 尺寸资源文件 -->
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
<dimen name="text_22">22sp</dimen>
<dimen name="text_16">16sp</dimen>
<dimen name="text_18">18sp</dimen>
<dimen name="margin_40">40dp</dimen>
<dimen name="margin_16">16dp</dimen>

</resources>
颜色资源文件:values/colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 颜色资源文件 -->
<color name="white" >#ffffff</color>

</resources>
主界面文件:layout/activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/main_bg"
android:orientation="vertical" >

<!-- 标题栏 -->

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/title_bg" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="@string/title"
android:textColor="@color/white"
android:textSize="@dimen/text_22"
android:textStyle="bold" />
</RelativeLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/margin_40"
android:orientation="vertical" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/margin_40"
android:gravity="center_horizontal"
android:orientation="horizontal" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/height"
android:textColor="@color/white"
android:textSize="@dimen/text_16" />

<EditText
android:id="@+id/et_height"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:inputType="numberDecimal"
android:singleLine="true"
android:textColor="@color/white"
android:textSize="@dimen/text_16" >
</EditText>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/cm"
android:textColor="@color/white"
android:textSize="@dimen/text_16" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/weight"
android:textColor="@color/white"
android:textSize="@dimen/text_16" />

<EditText
android:id="@+id/et_weight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:inputType="numberDecimal"
android:singleLine="true"
android:textColor="@color/white"
android:textSize="@dimen/text_16" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/kg"
android:textColor="@color/white"
android:textSize="@dimen/text_16" />
</LinearLayout>

<Button
android:id="@+id/btn_cala"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="@dimen/margin_16"
android:text="@string/cala"
android:textColor="@color/white"
android:textSize="@dimen/text_18" />

<LinearLayout
android:id="@+id/ll_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="@dimen/margin_16"
android:orientation="vertical" >

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >

<TextView
android:id="@+id/tv_bmi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/weight_bmi"
android:textColor="@color/white"
android:textSize="@dimen/text_16" />

<TextView
android:id="@+id/tv_state"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/weight_state"
android:textColor="@color/white"
android:textSize="@dimen/text_16" />
</LinearLayout>

<Button
android:id="@+id/btn_re_cala"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="@dimen/margin_16"
android:text="@string/re_cala"
android:textColor="@color/white"
android:textSize="@dimen/text_18" />
</LinearLayout>
</LinearLayout>

</LinearLayout>

功能开发

主界面管理类:src/
com.kedi.bmi.MainActivity.java
package com.kedi.bmi;

import java.text.DecimalFormat;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

/**
* BMI指数计算器主界面管理类
*
* @author 科弟
*
*/
@SuppressLint("ShowToast")
public class MainActivity extends Activity implements OnClickListener {
private EditText mHeightInputEt;// 身高输入框
private EditText mWeightInputEt;// 体重输入框
private Button mCalaBtn;// 计算按钮
private LinearLayout mResultLl;// 计算结果布局
private TextView mWeightBmiTv;// 体重指数文本
private TextView mWeightStateTv;// 体重状态文本
private Button mReCalaBtn;// 重新计算按钮
private double height;// 身高
private double weight;// 体重
private double bmi;// bmi指数值
private String state;// 体重状态
private String weightBmi;// 您的体重指数:
private String weightState;// 您的体重状况:
// 可取体重状态值数组
private String[] states = { "偏瘦", "正常", "偏胖", "肥胖", "重度肥胖", "极重度肥胖" };

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 去掉系统标题栏
requestWindowFeature(Window.FEATURE_NO_TITLE);
// 关联布局文件
setContentView(R.layout.activity_main);
// 初始化布局或控件
initView();
// 注册控件点击事件
setViewListener();
weightBmi = getResources().getString(R.string.weight_bmi);// 您的体重指数:
weightState = getResources().getString(R.string.weight_state);// 您的体重状况:

}

/**
* 初始化布局或控件的方法
*/
private void initView() {
mHeightInputEt = (EditText) this.findViewById(R.id.et_height);// 身高输入框
mWeightInputEt = (EditText) this.findViewById(R.id.et_weight);// 体重输入框
mCalaBtn = (Button) this.findViewById(R.id.btn_cala);// 计算按钮
mResultLl = (LinearLayout) this.findViewById(R.id.ll_result);// 计算结果布局
mWeightBmiTv = (TextView) this.findViewById(R.id.tv_bmi);// 体重指数文本
mWeightStateTv = (TextView) this.findViewById(R.id.tv_state);// 体重状态文本
mReCalaBtn = (Button) this.findViewById(R.id.btn_re_cala);// 重新计算按钮
// 默认显示计算按钮(VISIBLE),隐藏结果布局(GONE)
mCalaBtn.setVisibility(View.VISIBLE);
mResultLl.setVisibility(View.GONE);
}

/**
* 注册控件点击事件的方法
*/
private void setViewListener() {
mCalaBtn.setOnClickListener(this);
mReCalaBtn.setOnClickListener(this);
}

@Override
public void onClick(View v) {
int id = v.getId();
switch (id) {
case R.id.btn_cala:
// 计算逻辑
// 获取身高输入框数据
String heightStr = mHeightInputEt.getText().toString().trim();
// 判断身高输入框数据是否为空
if ("".equals(heightStr) || heightStr.length() == 0) {
Toast.makeText(this, getResources().getString(R.string.tip1), 0)
.show();
} else {
try {
// 将String类型转化成Double类型
height = Double.valueOf(heightStr);
String weightStr = mWeightInputEt.getText().toString()
.trim();

if ("".equals(weightStr) || weightStr.length() == 0) {
Toast.makeText(this,
getResources().getString(R.string.tip3),
Toast.LENGTH_SHORT).show();
} else {
try {
weight = Double.valueOf(weightStr);
// 计算bmi值
calaBmi();
// 隐藏计算按钮(GONE),显示结果布局(VISIBLE)
setViewVisible(false);
} catch (Exception e) {
Toast.makeText(this,
getResources().getString(R.string.tip2),
Toast.LENGTH_SHORT).show();
}

}

} catch (Exception e) {
Toast.makeText(this,
getResources().getString(R.string.tip2),
Toast.LENGTH_SHORT).show();

}

}

break;

case R.id.btn_re_cala:
// 重新计算逻辑
setViewVisible(true);
break;
}
}

/**
* 计算bmi值
*/
private void calaBmi() {
// height/100.0 cm换算成m
height = height / 100.0;
bmi = weight / (height * height);

if (bmi < 18.5) {
state = states[0];
} else if (bmi >= 18.5 && bmi <= 24.9) {
state = states[1];
} else if (bmi >24.9 && bmi <= 29.9) {
state = states[2];
} else if (bmi >29.9 && bmi <= 34.9) {
state = states[3];
} else if (bmi >34.9 && bmi <= 39.9) {
state = states[4];
} else {
state = states[5];
}

}

/**
* 控制计算按钮与结果布局的显示与隐藏
*
* @param visible
*/
private void setViewVisible(boolean visible) {
if (visible) {
mCalaBtn.setVisibility(View.VISIBLE);
mResultLl.setVisibility(View.GONE);
// 清空数据
mWeightBmiTv.setText("");
mWeightStateTv.setText("");
mHeightInputEt.setText("");
mWeightInputEt.setText("");
height = 0.0;
weight = 0.0;
} else {
//获得焦点
mHeightInputEt.requestFocus();
// 格式化数据的类
DecimalFormat format = new DecimalFormat("0.0");
mCalaBtn.setVisibility(View.GONE);
mResultLl.setVisibility(View.VISIBLE);
mWeightBmiTv.setText(weightBmi + format.format(bmi));
mWeightStateTv.setText(weightState + state);

}
}
}


源码下载

源码下载地址:http://download.csdn.net/detail/kedi_study/8934139
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: