(1)任务描述
BMI指数(即身体质量指数,简称体质指数又称体重,英文为Body Mass Index,简称BMI),是用体重公斤数除以身高米数平方得出的数字,是目前国际上常用的衡量人体胖瘦程度以及是否健康的一个标准。基于Android的TextView、EditText、RadioGroup、RadioButton、Button、ImageView等控件进行实现。
BMI计算方法:
体质指数(BMI)=体重(kg)÷身高^2(m)
EX:70kg÷(1.75×1.75)=22.86
结果判断标准:
男性: 女性:
BMI<16:偏瘦状态 BMI<18:偏瘦状态
16<=BMI<20:健康状态 18<=BMI<22:健康状态
20<=BMI<23:肥胖状态 22<=BMI<25:肥胖状态
BMI>=23 :重度肥胖 BMI>=25 :重度肥胖
按照应用绘制布局。并编写逻辑代码。
具体实现效果如下图所示:
activity_main.xml代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/sport">
<!--EditText中只能输入数值-->
<TextView
android:id="@+id/bmi_input_tv1"
android:text="身高(cm):"
android:textSize="22sp"
android:textStyle="bold"
android:layout_marginStart="30dp"
android:layout_marginTop="80dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:ignore="HardcodedText" />
<EditText
android:id="@+id/bmi_input_et_sg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toEndOf="@id/bmi_input_tv1"
android:layout_alignBottom="@id/bmi_input_tv1"
android:layout_marginEnd="30dp"
android:digits="0123456789"
tools:ignore="Autofill" />
<TextView
android:id="@+id/bmi_input_tv2"
android:text="体重(kg):"
android:textSize="22sp"
android:textStyle="bold"
android:layout_marginLeft="30dp"
android:layout_below="@id/bmi_input_tv1"
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/bmi_input_et_tz"
android:layout_toRightOf="@id/bmi_input_tv2"
android:layout_alignBottom="@id/bmi_input_tv2"
android:layout_marginRight="30dp"
android:digits="0123456789"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<RadioGroup
android:id="@+id/bmi_input_rg"
android:orientation="horizontal"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_below="@id/bmi_input_tv2"
android:layout_marginTop="20dp"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/bmi_input_rb_nan"
android:text="男"
android:checked="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:id="@+id/bmi_input_rb_nv"
android:text="女"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RadioGroup>
<Button
android:id="@+id/bmi_input_btn_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/bmi_input_rg"
android:layout_centerHorizontal="true"
android:text="开始测试"
android:padding="20dp"
android:layout_marginTop="20dp"
android:backgroundTint="@color/black"/>
<Button
android:id="@+id/bmi_input_btn_reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/bmi_input_btn_start"
android:layout_centerHorizontal="true"
android:text="重置"
android:layout_marginTop="20dp"
android:padding="20dp"
android:layout_alignLeft="@id/bmi_input_btn_start"
android:layout_alignRight="@id/bmi_input_btn_start"
android:backgroundTint="@color/black"/>
</RelativeLayout>
activity_healthy_result.xml代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#87CEEB"
tools:context=".HealthyResultActivity">
<ImageView
android:layout_marginTop="20dp"
android:id="@+id/bmi_res_iv"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_gravity="center"
android:src="@mipmap/bmi_1" />
<TextView
android:id="@+id/bmi_res_tv1"
android:text="您的bmi值为:24"
android:textStyle="bold"
android:textSize="22sp"
android:layout_marginTop="10dp"
android:textColor="#FF00FF"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
<TextView
android:id="@+id/bmi_res_tv2"
android:text="您目前的状态为:肥胖状态"
android:textStyle="bold"
android:textSize="22sp"
android:layout_marginTop="10dp"
android:textColor="#FF9912"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
<TextView
android:id="@+id/bmi_res_tv3"
android:text="您正常体重区间为:\n58.32kg~71.28kg"
android:textStyle="bold"
android:textSize="22sp"
android:layout_marginTop="10dp"
android:textColor="#F0FFF0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
</LinearLayout>
HealthyResultActivity.java代码
package com.example.healthy;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.text.DecimalFormat;
public class HealthyResultActivity extends AppCompatActivity {
int[] pic = {R.mipmap.bmi_3,
R.mipmap.bmi_4, R.mipmap.bmi_2, R.mipmap.bmi_1,};
ImageView img;
TextView BMI, zhaungtai, num1;
@SuppressLint("SetTextI18n")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_healthy_result);
img = (ImageView) findViewById(R.id.bmi_res_iv);
BMI = (TextView) findViewById(R.id.bmi_res_tv1);
zhaungtai = (TextView) findViewById(R.id.bmi_res_tv2);
num1 = (TextView) findViewById(R.id.bmi_res_tv3);
//接受上一个界面的传值
Intent intent = getIntent();
String sex = intent.getStringExtra("sex");
float num = intent.getFloatExtra("BMI", 0);
float height = intent.getFloatExtra("height", 0); //160cm
DecimalFormat df = new DecimalFormat("#.##");
if (sex.equals("女")) {
//体重区间计算
String fe_w1 = df.format(height * height * 16 / 10000);
String fe_w2 = df.format(height * height * 20 / 10000);
num1.setText("您正常体重区间为:\n" + fe_w1 + "kg--" + fe_w2 + "kg");
if (num < 16) {
img.setImageResource(pic[2]);
BMI.setText("您的bmi值为:" + df.format(num) + "");
zhaungtai.setText("您目前的状态为:偏瘦状态");
} else if (num >= 16 && num < 20) {
img.setImageResource(pic[3]);
BMI.setText("您的bmi值为:" + df.format(num) + "");
zhaungtai.setText("您目前的状态为:健康状态");
} else if (num >= 20 && num < 23) {
img.setImageResource(pic[2]);
BMI.setText("您的bmi值为:" + df.format(num) + "");
zhaungtai.setText("您目前的状态为:肥胖状态");
} else {
img.setImageResource(pic[3]);
BMI.setText("您的bmi值为:" + df.format(num) + "");
zhaungtai.setText("您目前的状态为:重度肥胖");
}
} else {
//体重区间计算
String fe_w1 = df.format(height * height * 18 / 10000);
String fe_w2 = df.format(height * height * 22 / 10000);
num1.setText(df.format(height * height * 20 / 10000));
num1.setText("您正常体重区间为:\n" + fe_w1 + "kg--" + fe_w2 + "kg");
if (num < 18) {
img.setImageResource(pic[0]);
BMI.setText("您的bmi值为:" + df.format(num) + "");
zhaungtai.setText("您目前的状态为:偏瘦状态");
} else if (num >= 18 && num < 22) {
img.setImageResource(pic[1]);
BMI.setText("您的bmi值为:" + df.format(num) + "");
zhaungtai.setText("您目前的状态为:健康状态");
} else if (num >= 22 && num < 25) {
img.setImageResource(pic[0]);
BMI.setText("您的bmi值为:" + df.format(num) + "");
zhaungtai.setText("您目前的状态为:肥胖状态");
} else {
img.setImageResource(pic[1]);
BMI.setText("您的bmi值为:" + df.format(num) + "");
zhaungtai.setText("您目前的状态为:重度肥胖");
}
}
}
}
MainActivity.java代码
package com.example.healthy;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
EditText heightEt, weightEt;
Button start, restart;
float bmi = 0;
RadioGroup sexchoose;
String sex_Str; //被选中的性别
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
heightEt = (EditText) findViewById(R.id.bmi_input_et_sg);
weightEt = (EditText) findViewById(R.id.bmi_input_et_tz);
start = (Button) findViewById(R.id.bmi_input_btn_start);
restart = (Button) findViewById(R.id.bmi_input_btn_reset);
sexchoose = (RadioGroup) findViewById(R.id.bmi_input_rg);
start.setOnClickListener(this);
restart.setOnClickListener(this);
}
@Override
public void onClick(View view) {
int i = view.getId();
Intent intent = new Intent();
// 获取被选中的单选按钮
int checkedId = sexchoose.getCheckedRadioButtonId();
RadioButton checkRb = (RadioButton) findViewById(checkedId);
sex_Str = checkRb.getText().toString();
String strHeight = heightEt.getText().toString();
String strWeight = weightEt.getText().toString();
int intHeight = Integer.parseInt(strHeight);
int intWeight = Integer.parseInt(strWeight);
if (R.id.bmi_input_btn_start == i) {
if (TextUtils.isEmpty(strHeight.trim())
|| TextUtils.isEmpty(strWeight.trim())) {
Toast.makeText(this, "身高或体重不能为空", Toast.LENGTH_SHORT).show();
return;
} else if (intWeight > 150 || intWeight < 10 || intHeight > 200 || intHeight < 40) {
Toast.makeText(this, "请输入的身高在40——200cm之间,体重在10——150kg", Toast.LENGTH_LONG).show();
return;
} else {
// 将身高体重的信息转换成整数类型
float height_num = Float.parseFloat(strHeight.trim());
float weight_num = Float.parseFloat(strWeight.trim());
bmi = (float) weight_num / (height_num * height_num / 10000);
Toast.makeText(
this,
"身高:" + heightEt.getText().toString().trim() + ","
+ "体重:" + weightEt.getText().toString().trim()
+ "," + "性别:" + sex_Str + "," + "BMI:" + bmi,
Toast.LENGTH_SHORT).show();
intent.setClass(this, HealthyResultActivity.class);
intent.putExtra("BMI", bmi);
intent.putExtra("sex", sex_Str);
intent.putExtra("height", height_num);
startActivity(intent);
}
}
if (R.id.bmi_input_btn_reset == i) {
heightEt.setText("");
weightEt.setText("");
}
}
}