Android's simple BMI calculator app

BMI Calculator description

Body mass index (BMI, Body Mass Index) is a commonly used measure of the degree of obesity and international human health standards are important, mainly for statistical analysis. The degree of obesity can not be used to determine the absolute value of body weight, which is related to the natural and height.

This is a BMI calculator interface

The formula

Square body mass index BMI = weight / height (IU kg / m)
BMI = weight / height * height

Guideline

BMI classification Chinese reference standard
Slim <18.5
normal 18.5~23.9
overweight ≥24
Overweight 24~26.9
obesity 27~29.9
Severe obesity ≥30

BMI interface design
activity_main.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:id="@+id/content_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/activity_main">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="24sp"
        android:textColor="#03A9F4"
        android:text="   身高(米):" />

    <EditText
        android:id="@+id/height"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入身高:" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="24sp"
        android:textColor="#03A9F4"
        android:text="   体重(公斤):" />

    <EditText
        android:id="@+id/weight"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入体重:" />

    <TextView
        android:id="@+id/Text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="24sp"
        android:textColor="#03A9F4"
        android:layout_margin="10dp"
        android:text="您的BMI为 :" />
    <TextView
        android:id="@+id/Text2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="24sp"
        android:textColor="#03A9F4" />
    <TextView
        android:id="@+id/Text3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="24sp"
        android:textColor="#03A9F4"
        android:layout_margin="10dp"
        android:text="身体状态 :" />
    <TextView
        android:id="@+id/Text4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="24sp"
        android:textColor="#03A9F4" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:textSize="24sp"
        android:textColor="#03A9F4"
        android:layout_margin="30dp"
        android:text="计算体质指数" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:textSize="24sp"
        android:textColor="#03A9F4"
        android:text="         清   除        " />
</LinearLayout>



I use a linear layout manager, a brief introduction of some properties
such as
android: textSize = "24sp" // set the font size
android: textColor = "# 03A9F4" // set the font color
android: layout_margin = "30dp" // Set Controls the margins for the 30DP
Android: layout_gravity = "center_horizontal" // set the control center level

interface display effect:
Interface display
Main_Activity.java

package com.example.s18_2;  //项目包名

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;  //导包
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.text.DecimalFormat;

public class MainActivity extends AppCompatActivity {
    Button button1, button2;
    EditText heightText, weightText;
    TextView BMIText, classificationText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);   
        button1 = findViewById(R.id.button);
        button2 = findViewById(R.id.button2);
        heightText = findViewById(R.id.height);
        weightText = findViewById(R.id.weight);
        BMIText = findViewById(R.id.Text2);
        classificationText = findViewById(R.id.Text4);
        //计算结果按键
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DecimalFormat nf = new DecimalFormat("0.00");  //保留两位小数点
                double weight, height, BMI;
                weight = Double.parseDouble(weightText.getText().toString());
                height = Double.parseDouble(heightText.getText().toString());
                BMI = weight / (height * height);   //计算公式
                BMIText.setText(nf.format(BMI));
                if (BMI < 20) {
                    classificationText.setText("偏瘦,多吃点东西");
                } else if (BMI > 25) {
                    classificationText.setText("超重,少吃点东西");
                } else {
                    classificationText.setText("正常,继续保持");
                }
            }
        });
        //清除控件里的内容
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                weightText.setText("");
                heightText.setText("");
                BMIText.setText("");
                classificationText.setText("");
            }
        });
    }
}

Run Run results demonstrate
The results show
learning the road we will study together, thank you.

Released four original articles · won praise 5 · Views 414

Guess you like

Origin blog.csdn.net/qq_45844792/article/details/105033079