Android开发实用小工具十三——BMI指数计算工具
生活随笔
收集整理的這篇文章主要介紹了
Android开发实用小工具十三——BMI指数计算工具
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 前言
- 一、效果展示
- 二、代碼
- 1.準備工作
- 2.樣式布局
- 3.“BMI指數計算”活動頁面
- 4.“BMI計算結果”活動頁面
- 總結
前言
BMI指數計算工具的開發與實現。
一、效果展示
二、代碼
1.準備工作
res/drawable/ic_back.xml :
<vector xmlns:android="http://schemas.android.com/apk/res/android"android:width="24dp"android:height="24dp"android:tint="@color/black"android:viewportWidth="24"android:viewportHeight="24"><pathandroid:fillColor="@android:color/white"android:pathData="M11.67,3.87L9.9,2.1 0,12l9.9,9.9 1.77,-1.77L3.54,12z" /> </vector>res/drawable/shape_module.xml :
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"><!-- 指定了形狀內部的填充顏色 --><solid android:color="@color/white" /><!-- 指定了形狀四個圓角的半徑 --><cornersandroid:bottomRightRadius="10dp"android:bottomLeftRadius="10dp"android:topLeftRadius="10dp"android:topRightRadius="10dp" /></shape>res/drawable/shape_button.xml :
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"><!-- 指定了形狀內部的填充顏色 --><solid android:color="#fcfcfc" /><!-- 指定了形狀輪廓的粗細與顏色 --><strokeandroid:width="1dp"android:color="#D6D6D6" /><!-- 指定了形狀四個圓角的半徑 --><corners android:radius="100dp" /></shape>有關性別選擇的四張圖片(man_0, man_1, woman_0, woman_1) :
有關BMI計算結果的四張圖片(bmi_0, bmi_1, bmi_2, bmi_3) :
2.樣式布局
res/layout/activity_bmi_calculate.xml :
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:background="#fefefe"android:orientation="vertical"><RelativeLayout android:layout_width="match_parent"android:layout_height="50dp"android:background="#FFFFFFFF"><ImageViewandroid:id="@+id/iv_back"android:layout_width="40dp"android:layout_height="match_parent"android:layout_alignParentLeft="true"android:padding="10dp"android:scaleType="fitCenter"android:src="@drawable/ic_back" /><TextViewandroid:id="@+id/tv_title"android:layout_width="wrap_content"android:layout_height="match_parent"android:layout_centerInParent="true"android:gravity="center"android:textColor="#FF000000"android:textSize="17sp"android:text="BMI指數計算" /></RelativeLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:orientation="vertical"><ScrollViewandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"android:padding="25dp"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginVertical="8dp"android:orientation="horizontal"><TextViewandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="3"android:text="年齡"android:textColor="#FF8C90A9"android:textSize="14sp" /><TextViewandroid:id="@+id/tv_sex"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="2"android:text="性別(男)"android:textColor="#FF8C90A9"android:textSize="14sp" /></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="70dp"android:layout_marginVertical="8dp"android:orientation="horizontal"><LinearLayoutandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="3"><EditTextandroid:id="@+id/et_age"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_marginRight="60dp"android:layout_weight="3"android:hint="請輸入年齡"android:inputType="number"android:maxLength="3"android:paddingVertical="20dp"android:textColor="#FF000000"android:textColorHint="#FFD6D6D6"android:textSize="22sp" /></LinearLayout><LinearLayoutandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="2"><LinearLayoutandroid:id="@+id/ll_sex_1"android:layout_width="0dp"android:layout_height="match_parent"android:layout_marginRight="20dp"android:layout_weight="1"android:background="@drawable/shape_module"android:backgroundTint="#FFD9EBFF"android:padding="15dp"><ImageViewandroid:id="@+id/iv_sex_1"android:layout_width="match_parent"android:layout_height="match_parent"android:src="@drawable/man_1" /></LinearLayout><LinearLayoutandroid:id="@+id/ll_sex_2"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:background="@drawable/shape_module"android:backgroundTint="#FFF5F5F5"android:padding="15dp"><ImageViewandroid:id="@+id/iv_sex_2"android:layout_width="match_parent"android:layout_height="match_parent"android:src="@drawable/woman_0" /></LinearLayout></LinearLayout></LinearLayout><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginVertical="8dp"android:text="身高(厘米)"android:textColor="#FF8C90A9"android:textSize="14sp" /><EditTextandroid:id="@+id/et_height"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginVertical="8dp"android:hint="請輸入身高"android:inputType="numberDecimal"android:maxLength="6"android:paddingVertical="20dp"android:textColor="#FF000000"android:textColorHint="#FFD6D6D6"android:textSize="22sp" /><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginVertical="8dp"android:text="體重(公斤)"android:textColor="#FF8C90A9"android:textSize="14sp" /><EditTextandroid:id="@+id/et_weight"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginVertical="8dp"android:hint="請輸入體重"android:inputType="numberDecimal"android:maxLength="6"android:paddingVertical="20dp"android:textColor="#FF000000"android:textColorHint="#FFD6D6D6"android:textSize="22sp" /><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="40dp"android:text="BMI指數:"android:textColor="#FF666666"android:textSize="14sp"android:textStyle="bold" /><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="5dp"android:text="簡稱體質指數,是通過體重公斤除以身高米數平方得出的數字。是目前國際上常用的衡量人體胖瘦程度以及是否健康的一個標椎。"android:textColor="#FF666666"android:textSize="14sp" /><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="15dp"android:gravity="center"android:text="BMI=體重(公斤)/身高(米)2"android:textColor="#FF666666"android:textSize="17sp" /></LinearLayout></ScrollView><Buttonandroid:id="@+id/btn_calculate"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginHorizontal="35dp"android:layout_marginVertical="15dp"android:background="@drawable/shape_button"android:backgroundTint="#FFFF8800"android:text="開始計算"android:textColor="#FFFFFFFF"android:textSize="17sp" /></LinearLayout></LinearLayout>res/layout/activity_bmi_result.xml :
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:background="#fefefe"android:orientation="vertical"><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="50dp"android:background="#FFFFFFFF"><ImageViewandroid:id="@+id/iv_back"android:layout_width="40dp"android:layout_height="match_parent"android:layout_alignParentLeft="true"android:padding="10dp"android:scaleType="fitCenter"android:src="@drawable/ic_back" /><TextViewandroid:id="@+id/tv_title"android:layout_width="wrap_content"android:layout_height="match_parent"android:layout_centerInParent="true"android:gravity="center"android:textColor="#FF000000"android:textSize="17sp"android:text="BMI計算結果" /></RelativeLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"android:padding="25dp"><TextViewandroid:id="@+id/tv_bmi"android:layout_width="match_parent"android:layout_height="wrap_content"tools:text="26.1"android:textColor="#FF000000"android:textSize="60sp" /><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="身高體重指數"android:layout_marginTop="10dp"android:textColor="#FF666666"android:textSize="14sp" /><ImageViewandroid:id="@+id/iv_result"android:layout_width="match_parent"android:layout_height="wrap_content"android:src="@drawable/bmi_3"/><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="數據分析"android:layout_marginTop="30dp"android:layout_marginBottom="15dp"android:textColor="#FF8C90A9"android:textSize="13sp" /><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginVertical="15dp"android:orientation="horizontal"><TextViewandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:text="身高(厘米)"android:textColor="#FF000000"android:textSize="14sp" /><TextViewandroid:id="@+id/tv_height"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:gravity="right"tools:text="175.00cm"android:textColor="#FF666666"android:textSize="14sp" /></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginVertical="15dp"android:orientation="horizontal"><TextViewandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:text="建議體重(公斤)"android:textColor="#FF000000"android:textSize="14sp" /><TextViewandroid:id="@+id/tv_weight"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:gravity="right"tools:text="56.70 ~ 73.50kg"android:textColor="#FF666666"android:textSize="14sp" /></LinearLayout></LinearLayout></LinearLayout>3.“BMI指數計算”活動頁面
BMICalculateActivity.java :
public class BMICalculateActivity extends AppCompatActivity implements View.OnClickListener {ImageView iv_sex_1;ImageView iv_sex_2;LinearLayout ll_sex_1;LinearLayout ll_sex_2;TextView tv_sex;EditText et_age;EditText et_height;EditText et_weight;Button btn_calculate;// 當前選擇的性別。false 男,true 女boolean sex = false;// 校驗輸入的正則表達式String regex = "[1-9][0-9]{0,2}(\\.[0-9]{0,2}){0,1}";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_bmi_calculate);// 獲取控件,并添加點擊事件findViewById(R.id.iv_back).setOnClickListener(this);iv_sex_1 = findViewById(R.id.iv_sex_1);iv_sex_2 = findViewById(R.id.iv_sex_2);ll_sex_1 = findViewById(R.id.ll_sex_1);ll_sex_2 = findViewById(R.id.ll_sex_2);tv_sex = findViewById(R.id.tv_sex);et_age = findViewById(R.id.et_age);et_height = findViewById(R.id.et_height);et_weight = findViewById(R.id.et_weight);btn_calculate = findViewById(R.id.btn_calculate);ll_sex_1.setOnClickListener(this);ll_sex_2.setOnClickListener(this);btn_calculate.setOnClickListener(this);// 添加InputFilter,對輸入進行校驗et_height.setFilters(new MyInputFilter[]{new MyInputFilter()});et_weight.setFilters(new MyInputFilter[]{new MyInputFilter()});}@Overridepublic void onClick(View v) {switch (v.getId()) {// 點擊了返回按鈕case R.id.iv_back:finish();break;// 點擊了性別男case R.id.ll_sex_1:if (sex) {tv_sex.setText("性別(男)");ll_sex_1.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor("#FFD9EBFF")));iv_sex_1.setImageResource(R.drawable.man_1);ll_sex_2.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor("#FFF5F5F5")));iv_sex_2.setImageResource(R.drawable.woman_0);sex = false;}break;// 點擊了性別女case R.id.ll_sex_2:if (!sex) {tv_sex.setText("性別(女)");ll_sex_1.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor("#FFF5F5F5")));iv_sex_1.setImageResource(R.drawable.man_0);ll_sex_2.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor("#FFFEE0E0")));iv_sex_2.setImageResource(R.drawable.woman_1);sex = true;}break;// 點擊了計算按鈕case R.id.btn_calculate:String age = et_age.getText().toString();String height = et_height.getText().toString();String weight = et_weight.getText().toString();if (age.equals("") || height.equals("") || weight.equals("")) {Toast.makeText(this, "請填寫完整的參數", Toast.LENGTH_SHORT).show();break;}if (Integer.parseInt(age) < 14) {Toast.makeText(this, "14周歲以下暫不提供BMI計算服務", Toast.LENGTH_SHORT).show();break;}Intent intent = new Intent();// 創建一個新包裹Bundle bundle = new Bundle();// 跳轉活動頁面頁面intent.setClass(this, BMIResultActivity.class);bundle.putDouble("height", Double.parseDouble(height));bundle.putDouble("weight", Double.parseDouble(weight));intent.putExtras(bundle);startActivity(intent);break;}}private class MyInputFilter implements InputFilter {@Overridepublic CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {String s = dest.toString() + source;if (!s.matches(regex))return "";return source;}} }4.“BMI計算結果”活動頁面
BMIResultActivity.java :
public class BMIResultActivity extends AppCompatActivity implements View.OnClickListener {ImageView iv_result;TextView tv_bmi;TextView tv_height;TextView tv_weight;// 上個頁面傳來的身高體重參數double height;double weight;// BMI計算結果double bmi;// 建議體重double suggest_weight_1;double suggest_weight_2;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_bmi_result);// 從上一個頁面傳來的意圖中獲取快遞包裹Bundle bundle = getIntent().getExtras();height = bundle.getDouble("height");weight = bundle.getDouble("weight");// 獲取控件,并添加點擊事件findViewById(R.id.iv_back).setOnClickListener(this);iv_result = findViewById(R.id.iv_result);tv_bmi = findViewById(R.id.tv_bmi);tv_height = findViewById(R.id.tv_height);tv_weight = findViewById(R.id.tv_weight);tv_height.setText(String.format("%.2fcm", height));BigDecimal bd1 = new BigDecimal(height / 100);BigDecimal bd2 = new BigDecimal(weight);bmi = bd2.divide(bd1.multiply(bd1), 2, BigDecimal.ROUND_HALF_UP).doubleValue();tv_bmi.setText(String.valueOf(bmi));if (bmi < 18.5)iv_result.setImageResource(R.drawable.bmi_0);else if (bmi >= 18.5 && bmi <= 24.0)iv_result.setImageResource(R.drawable.bmi_1);else if (bmi > 24.0 && bmi <= 28.0)iv_result.setImageResource(R.drawable.bmi_2);elseiv_result.setImageResource(R.drawable.bmi_3);suggest_weight_1 = new BigDecimal(18.5).multiply(bd1.multiply(bd1)).doubleValue();suggest_weight_2 = new BigDecimal(24.0).multiply(bd1.multiply(bd1)).doubleValue();tv_weight.setText(String.format("%.2f ~ %.2fkg", suggest_weight_1, suggest_weight_2));}@Overridepublic void onClick(View v) {switch (v.getId()) {// 點擊了返回按鈕case R.id.iv_back:finish();break;}} }總結
以上就是BMI指數計算工具的開發與實現的內容。
總結
以上是生活随笔為你收集整理的Android开发实用小工具十三——BMI指数计算工具的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: jQuery滑块拼图验证插件
- 下一篇: Android开发必备之Picasso加