【BMI指数计算器V1.0】项目实战
項目背景
? ? ? ??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-sdkandroid:minSdkVersion="14"android:targetSdkVersion="19" /><applicationandroid:allowBackup="true"android:icon="@drawable/ic_launcher"android:label="@string/app_name"android:theme="@style/AppTheme" ><activityandroid: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" ><!-- 標題欄 --><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:background="@drawable/title_bg" ><TextViewandroid: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><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="@dimen/margin_40"android:orientation="vertical" ><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="@dimen/margin_40"android:gravity="center_horizontal"android:orientation="horizontal" ><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/height"android:textColor="@color/white"android:textSize="@dimen/text_16" /><EditTextandroid: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><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/cm"android:textColor="@color/white"android:textSize="@dimen/text_16" /></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center_horizontal"android:orientation="horizontal" ><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/weight"android:textColor="@color/white"android:textSize="@dimen/text_16" /><EditTextandroid: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" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/kg"android:textColor="@color/white"android:textSize="@dimen/text_16" /></LinearLayout><Buttonandroid: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" /><LinearLayoutandroid: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" ><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="vertical" ><TextViewandroid: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" /><TextViewandroid: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><Buttonandroid: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 = { "偏瘦", "正常", "偏胖", "肥胖", "重度肥胖", "極重度肥胖" };@Overrideprotected 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);}@Overridepublic 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換算成mheight = 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總結
以上是生活随笔為你收集整理的【BMI指数计算器V1.0】项目实战的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 技术干货 | 录屏采集实现教程 —— i
- 下一篇: 编程探究智能手机的图案解锁