bmi体质指数计算方法_正常的体质指数是多少[通俗易懂]

bmi体质指数计算方法_正常的体质指数是多少[通俗易懂]目标:开发一款BMI体质指数计算器

目标:开发一款BMI体质指数计算器。用户在主界面中输入身高和体重,单击“计算BMI值”按钮后,在界面二通过TextView显示相应的结论;界面二点击返回能够回到主界面。

体质指数与胖瘦程度表

胖瘦程度 体质指数
过轻 男性低于20,女性低于19
适中 男性20—25,女性19—24
超重 男性25—30,女性24—29
肥胖 男性20—25,女性19—24
严重肥胖 男性高于35,女性高于34

BMI是体质指数,一种公认的评定个人体质胖瘦程度的分级方法,具体的计算方法如下:
体质指数(BMI)=体重(kg)/身高(m)^2

实现代码如下:
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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/img04"
    tools:context="com.example.jinjin.bimtest.MainActivity"
    android:weightSum="1">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="身高(cm)" />
    <EditText
        android:id="@+id/height"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="体重(kg)" />
    <EditText
        android:id="@+id/weight"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <RadioGroup
        android:id="@+id/radioGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <RadioButton
            android:id="@+id/btnMan"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="男"
            android:checked="true"/>
        <RadioButton
            android:id="@+id/btnWoman"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="女"/>
    </RadioGroup>
    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="150dp"
        android:layout_marginTop="100dp"
        android:background="#E6E6FA"
        android:text="计算BMI值" />
</LinearLayout>

activity_second.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:background="@drawable/img04"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#5F9EA0"
        android:textSize="20dp"
        android:layout_marginLeft="90dp"
        android:layout_marginTop="80dp"
        android:text="文本" />
    <Button
        android:id="@+id/btnback"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="155dp"
        android:layout_marginTop="135dp"
        android:background="#E6E6FA"
        android:text="返回"/>
</LinearLayout>

MainActivity.java

package com.example.jinjin.bimtest;

import android.content.Intent;
import android.icu.text.DecimalFormat;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;

public class MainActivity extends AppCompatActivity { 
   
    Button button;
    EditText height;
    EditText weight;
    RadioButton boy;
    RadioButton girl;

    @Override
    protected void onCreate(Bundle savedInstanceState) { 
   
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        height = (EditText) findViewById(R.id.height);
        weight = (EditText) findViewById(R.id.weight);
        boy = (RadioButton) findViewById(R.id.btnMan);
        girl = (RadioButton) findViewById(R.id.btnWoman);
        button = (Button) findViewById(R.id.btn);
        button.setOnClickListener(new mclick());
    }
    
    class mclick implements View.OnClickListener { 
   
        @RequiresApi(api = Build.VERSION_CODES.N)
        @Override
        public void onClick(View view) { 
   
            String h = height.getText().toString();
            String w = weight.getText().toString();
            String msg = "";
            String bmi = "";
            double res = 0, heightNum = 0, weightNum = 0;
            if (!h.isEmpty() && !w.isEmpty()) { 
   
                heightNum = Double.parseDouble(h);  // 强制类型转换,把括号里的类型变成double
                weightNum = Double.parseDouble(w);
                res = 10000 * weightNum / (heightNum * heightNum);
                DecimalFormat df = new DecimalFormat("#.00");
                bmi = df.format(res);
                System.out.println(bmi);
            }
            Log.i("bmi",bmi);
            msg="你的BMI值是".toString();
                if (boy.isChecked()) { 
   
                    if (res>35){ 
   
                        msg+=bmi+"\n"+"你的体型过于肥胖!要减肥了!".toString();
                    }else if (res>30){ 
   
                        msg+=bmi+"\n"+"你的体型是肥胖!要减肥了!".toString();
                    }else if (res>25){ 
   
                        msg+=bmi+"\n"+"你的体型是过重!可以减减肥了!".toString();
                    }else if (res>20){ 
   
                        msg+=bmi+"\n"+"你的体型十分匀称呀!".toString();
                    }else{ 
   
                        msg+=bmi+"\n"+"你的体型是过轻!要多吃点!".toString();
                    }
                }

                if (girl.isChecked()) { 
   
                    if (res>34){ 
   
                        msg+=bmi+"\n"+"你的体型过于肥胖!要减肥了!".toString();
                    }else if (res>29){ 
   
                        msg+=bmi+"\n"+"你的体型是肥胖!要减肥了!".toString();
                    }else if (res>24){ 
   
                        msg+=bmi+"\n"+"你的体型是过重!可以减减肥了!".toString();
                    }else if (res>19){ 
   
                        msg+=bmi+"\n"+"你的体型十分匀称呀!".toString();
                    }else{ 
   
                        msg+=bmi+"\n"+"你的体型是过轻!要多吃点!".toString();
                    }
                }
            System.out.println(msg);
            Intent intent=new Intent(MainActivity.this,SecondActivity.class);
            Bundle bundle=new Bundle();
            bundle.putString("msg",msg);
            intent.putExtras(bundle);
            startActivity(intent);
        }
    }
}

SecondActivity.java

package com.example.jinjin.bimtest;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
/** * Created by jinjin on 2020/5/17. */

public class SecondActivity extends AppCompatActivity { 
   
    TextView textView;
    Button btnback;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) { 
   
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        textView = (TextView) findViewById(R.id.textView);
        Bundle bundle = this.getIntent().getExtras();
        String msg = bundle.getString("msg");
        textView.setText(msg);
        btnback = (Button) findViewById(R.id.btnback);
        btnback.setOnClickListener(new View.OnClickListener() { 
   
            @Override
            public void onClick(View view) { 
   
                Intent intent = new Intent(SecondActivity.this, MainActivity.class);
                startActivity(intent);
            }
        });
    }
}

运行结果图
在这里插入图片描述
主界面

在这里插入图片描述
界面二

今天的文章bmi体质指数计算方法_正常的体质指数是多少[通俗易懂]分享到此就结束了,感谢您的阅读。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/64826.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注