前言
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">
<path
android: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" />
<!-- 指定了形状四个圆角的半径 -->
<corners
android: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" />
<!-- 指定了形状轮廓的粗细与颜色 -->
<stroke
android: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">
<ImageView
android: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" />
<TextView
android: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>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="25dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginVertical="8dp"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="年龄"
android:textColor="#FF8C90A9"
android:textSize="14sp" />
<TextView
android: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>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_marginVertical="8dp"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3">
<EditText
android: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>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2">
<LinearLayout
android: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">
<ImageView
android:id="@+id/iv_sex_1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/man_1" />
</LinearLayout>
<LinearLayout
android: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">
<ImageView
android:id="@+id/iv_sex_2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/woman_0" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginVertical="8dp"
android:text="身高(厘米)"
android:textColor="#FF8C90A9"
android:textSize="14sp" />
<EditText
android: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" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginVertical="8dp"
android:text="体重(公斤)"
android:textColor="#FF8C90A9"
android:textSize="14sp" />
<EditText
android: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" />
<TextView
android: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" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="简称体质指数,是通过体重公斤除以身高米数平方得出的数字。是目前国际上常用的衡量人体胖瘦程度以及是否健康的一个标椎。"
android:textColor="#FF666666"
android:textSize="14sp" />
<TextView
android: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>
<Button
android: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">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#FFFFFFFF">
<ImageView
android: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" />
<TextView
android: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>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="25dp">
<TextView
android:id="@+id/tv_bmi"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:text="26.1"
android:textColor="#FF000000"
android:textSize="60sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="身高体重指数"
android:layout_marginTop="10dp"
android:textColor="#FF666666"
android:textSize="14sp" />
<ImageView
android:id="@+id/iv_result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/bmi_3"/>
<TextView
android: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" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginVertical="15dp"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="身高(厘米)"
android:textColor="#FF000000"
android:textSize="14sp" />
<TextView
android: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>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginVertical="15dp"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="建议体重(公斤)"
android:textColor="#FF000000"
android:textSize="14sp" />
<TextView
android: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}";
@Override
protected 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()});
}
@Override
public 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 {
@Override
public 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;
@Override
protected 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);
else
iv_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));
}
@Override
public void onClick(View v) {
switch (v.getId()) {
// 点击了返回按钮
case R.id.iv_back:
finish();
break;
}
}
}
总结
以上就是BMI指数计算工具的开发与实现的内容。