Day15 - BMI计算机

BMI(身体质量指数)是用自己身高、体重的比例,来当作是否过胖的「身高体重指数」
它的计算方法也很简单
BMI = 体重(公斤)/身高平方(公尺)
台湾人体重习惯以公斤为单位所以没问题
但要注意的是台湾身高几乎都以公分为单位,这边需要把公分转为公尺再来做计算
人的标准BMI是介於18.5≦BMI<24之间
我们就来用之前学过得技术
做出BMI计算机八

开始

我们需要的元件有:
TextView x 3 (体重(公斤) x1 、身高(公分) x1 、诊断结果)
Button x 1 (计算 x1)
EditText x 2 (体重输入栏 x1 、身高输入栏 x1)
使用者输入身高时,只需要以公分输入,我们会写程序把公分转为公尺,好让用户方便使用
我们先把页面刻好
id设置:
体重的EditText:et_weight
身高的EditText:et_height
计算的Button:btn_cale
显示结果的TextView:tv_result
这边要注意:必须把EditText的inputType设成number
这样才不会有输入数字之外的字

activity_main.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:orientation="vertical"
    tools:context=".MainActivity">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="0dp"
            android:layout_weight=".2"
            android:layout_height="wrap_content"
            android:text="体重kg:"
            android:textAlignment="center"
            android:textSize="15dp"/>
        <EditText
            android:id="@+id/et_weight"
            android:layout_width="0dp"
            android:inputType="number"
            android:layout_weight=".8"
            android:layout_height="wrap_content"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="0dp"
            android:layout_weight=".2"
            android:layout_height="wrap_content"
            android:text="身高cm:"
            android:textAlignment="center"
            android:textSize="15dp"/>
        <EditText
            android:id="@+id/et_height"
            android:layout_width="0dp"
            android:inputType="number"
            android:layout_weight=".8"
            android:layout_height="wrap_content"/>
    </LinearLayout>
    <Button
        android:id="@+id/btn_cale"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="计算"/>
    <TextView
        android:id="@+id/tv_result"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:textSize="30dp"
        android:text="BMI:"
        android:textAlignment="center"/>

</LinearLayout>

MainActivity.java

除了体重、身高的TextView不用宣告外,其余的皆会使用到,所以必须宣告
并且抓取物件

EditText et_weight = findViewById(R.id.et_weight);
EditText et_height = findViewById(R.id.et_height);
Button btn_cale = findViewById(R.id.btn_cale);
TextView tv_result = findViewById(R.id.tv_result);

不管事抓EditText的值,或是最後BMI的结果都是再按下按钮後才会执行
所以把所有的程序都写在按钮点击事件内就好

btn_cale.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

    }
});

先抓到EditText体重和身高的值
运算BMI:体重(公斤)/身高平方(公尺)显示BMI的值
然後判断你BMI显示过高或过低

package com.example.bmi;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.text.NumberFormat;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        EditText et_weight = findViewById(R.id.et_weight);
        EditText et_height = findViewById(R.id.et_height);
        Button btn_cale = findViewById(R.id.btn_cale);
        TextView tv_result = findViewById(R.id.tv_result);

        btn_cale.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(et_weight.getText().length()!=0||et_weight.getText().length()!=0){
                    float weight = Float.parseFloat(et_weight.getText().toString());
                    float height = Float.parseFloat(et_height.getText().toString());
                    float bmi = weight/((height/100)*(height/100));
                    NumberFormat numberFormat = NumberFormat.getInstance();
                    numberFormat.setMaximumFractionDigits(2);
                    tv_result.setText("BMI:"+numberFormat.format(bmi));
                    if(bmi<18.5){
                        tv_result.setText(tv_result.getText()+"\n太瘦");
                    }
                    else if(bmi>=24){
                        tv_result.setText(tv_result.getText()+"\n太胖拉");
                    }
                    else if (bmi<24&& bmi>=18.5){
                        tv_result.setText(tv_result.getText()+"\n完美体态");
                    }
                }
                else{
                    Toast.makeText(MainActivity.this,"请输入身高/体重",Toast.LENGTH_SHORT).show();
                }

            }
        });
    }
}

执行结果:
https://ithelp.ithome.com.tw/upload/images/20210923/20141769uva0s0sOnC.png


<<:  Day 14. 模板语法Template Syntax – 插值 Attribute、JavaScript 表达式

>>:  Day26 Lab 2 - Object storage 上传续传

自动化测试,让你上班拥有一杯咖啡的时间 | Day 29 - cypress 最佳实践

此系列文章会同步发文到个人部落格,有兴趣的读者可以前往观看喔。 选取元素 ⚠️ 避免使用会常常变的s...

【程序】陷入低潮 转生成恶役菜鸟工程师避免 Bad End 的 30 件事 - 23

https://youtu.be/vpwC347cXog 陷入低潮 了解低潮 专注在可控的短期 充...

Day 1 Docker 初探

话说从前 自从电脑问世以来,若要将程序无缝的转移到其他机器上成功运行,那麽环境架设就是一个必须要优先...

Day21 - 使用Django进行自动化测试 (3)

今天的实作内容主要根据教学网站进行。 接续前两天的内容,今天将实作view的测试程序。 因为view...

开源网路钓鱼框架-Gophish(下)

Gophish钓鱼的寄送信件流程如下图 New Campaigns 钓鱼事件 完成前面的设定後接着选...