文章目录

  • 前言
  • 一、效果展示
  • 二、代码
    • 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=体重(公斤)/身高(米)²"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指数计算工具相关推荐

  1. Android开发实用小工具三——面积转换工具

    文章目录 前言 一.效果展示 二.代码 主代码 总结 前言 面积转换工具的开发与实现. 一.效果展示 二.代码 准备工作和样式布局与我开发的另一个小工具(长度转换工具)所用一致,详情请看我上一篇文章. ...

  2. Android开发实用小工具十一——亲戚称呼计算器

    文章目录 前言 一.效果展示 二.代码 1.样式布局 2.获取亲戚关系表 3.主代码 总结 前言 亲戚称呼计算器的开发与实现. 一.效果展示 二.代码 准备工作与我开发的另一个小工具(长度转换工具)所 ...

  3. Android开发实用小工具九——温度转换工具

    文章目录 前言 一.效果展示 二.代码 1.样式布局 2.主代码 总结 前言 温度转换工具的开发与实现. 一.效果展示 二.代码 准备工作与我开发的另一个小工具(长度转换工具)所用一致,详情请看我上一 ...

  4. Android开发实用小工具十——进制转换工具

    文章目录 前言 一.效果展示 二.代码 1.样式布局 2.主代码 总结 前言 进制转换工具的开发与实现. 一.效果展示 二.代码 准备工作与我开发的另一个小工具(长度转换工具)所用一致,详情请看我上一 ...

  5. Android开发实用小工具五——速度转换工具

    文章目录 前言 一.效果展示 二.代码 主代码 总结 前言 速度转换工具的开发与实现. 一.效果展示 二.代码 准备工作和样式布局与我开发的另一个小工具(长度转换工具)所用一致,详情请看我上一篇文章. ...

  6. Android 开发实用小技巧

    转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/75647437 本文出自[赵彦军的博客] Android Studio 实用插件传送 ...

  7. Android开发实用小技巧九——内嵌WebView的使用(内置浏览器)

    文章目录 前言 一.效果展示 二.代码 1.样式布局 2.活动页面 总结 前言 内嵌WebView的使用(内置浏览器). 一.效果展示 二.代码 1.样式布局 res/layout/activity_ ...

  8. Android开发实用小技巧六——根据网址跳转第三方浏览器

    文章目录 前言 代码 总结 前言 根据网址跳转第三方浏览器. 代码 方法 : // 根据网址跳转第三方浏览器 public static void jumpUriToBrowser(Context c ...

  9. Android开发笔记(六十三)HTTP访问的通信方式

    InputStream和OutputStream 输入输出流在java中很常用,从文件读写到内存读写到网络通信都会用到.在之前的< Android开发笔记(三十三)文本文件和图片文件的读写> ...

最新文章

  1. 多角度解析自动驾驶芯片
  2. Yii “CDbConnection failed to open the DB connection: could not find driver解决办法
  3. Linux_用户权限管理
  4. 简单Unity时间架构设计(克洛诺斯之匙)
  5. BZOJ 3085: 反质数加强版SAPGAP (反素数搜索)
  6. python的imread、newaxis
  7. Python 线性回归
  8. 夺命雷公狗---PHP开发APP接口---1(手动编写json)
  9. 《Python Cookbook 3rd》笔记(1.1):拆分序列后赋值给多个变量
  10. SpringBoot集成Shiro进行权限控制和管理
  11. 史上最全的JFinal源码分析(不间断更新)
  12. 国家航天局+华为云:做地球的守护者
  13. MYSQL必知必会-where语句
  14. arm中断保护和恢复_ARM中断处理过程
  15. JavaWeb:tomcat知识以及遇到的一些小问题
  16. 更改图书信息c语言编码,图书管理系统C语言实现源代码.pdf
  17. ichat模块schedule模块运行遇到的错误
  18. C语言热电阻温度查表,热电阻分度表如何看?是什么意思?
  19. 移动机器人五种坐标系
  20. 工业质检-缺陷检测数据集

热门文章

  1. 君似梅花我似叶(转载)
  2. Qt:调用OpenCV
  3. 打卡day02 python基础—列表
  4. 磁盘提示无法访问磁盘未被格式化,里面的资料怎样寻回
  5. 梅科尔工作室-寇涵冰-鸿蒙笔记4
  6. Mac Mojave python 3.8.6实验tensorflow 2.4
  7. 酒桌上的DOTA原理
  8. java byte 释放内存_java java.nio.ByteBuffer.allocateDirect 导致内存泄露
  9. [策略模式]在游戏开发中的应用
  10. 印章识别检测_印章尺寸测量_印章文字识别