zoukankan      html  css  js  c++  java
  • 12 BMI计算器

    BMI计算题(半成品)

    运行截图

    代码:

    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:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity" >
    
        <TextView
            android:id="@+id/textView6"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/BmiTitle"
            android:textSize="24sp" />
    
        <EditText
            android:id="@+id/editHeight"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="@string/BodyHeight"
            android:inputType="numberDecimal" />
    
        <EditText
            android:id="@+id/editWeight"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="@string/BodyWeight"
            android:inputType="numberDecimal" />
    
        <RadioGroup
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
    
            <RadioButton
                android:id="@+id/rbtnStdWho"
                android:layout_width="137dp"
                android:layout_height="wrap_content"
                android:text="@string/STD_WHO" />
    
            <RadioButton
                android:id="@+id/rbtnStdAsia"
                android:layout_width="137dp"
                android:layout_height="match_parent"
                android:text="@string/STD_ASIA" />
    
            <RadioButton
                android:id="@+id/rbtnStdChn"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/STD_CHINA" />
        </RadioGroup>
    
        <RadioGroup
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
    
            <RadioButton
                android:id="@+id/radioButton5"
                android:layout_width="137dp"
                android:layout_height="wrap_content"
                android:text="@string/LbIDisplayBMI" />
    
            <RadioButton
                android:id="@+id/radioButton6"
                android:layout_width="137dp"
                android:layout_height="wrap_content"
                android:text="@string/LbIDisplayReslut" />
    
            <RadioButton
                android:id="@+id/radioButton7"
                android:layout_width="137dp"
                android:layout_height="wrap_content"
                android:text="@string/LbIDisplayBoth" />
        </RadioGroup>
    
        <TextView
            android:id="@+id/txtResult"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/Result"
            tools:text="@string/Result" />
    
        <Button
            android:id="@+id/btnBmiCalc"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="计算" />
    
    </LinearLayout>

    MainActivity.java

    package com.example.helloworld;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.RadioButton;
    import android.widget.TextView;
    
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Button btnBmiCalc = (Button)findViewById(R.id.btnBmiCalc);
            btnBmiCalc.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v){
                    EditText editHeight = (EditText)findViewById(R.id.editHeight);
                    EditText editWeight = (EditText)findViewById(R.id.editWeight);
                    TextView txtResult = (TextView)findViewById(R.id.txtResult);
                    Double height = Double.parseDouble(editHeight.getText().toString());
                    Double weight = Double.parseDouble(editWeight.getText().toString());
                    Double bmi = weight/(height * height);
                    RadioButton rbtnStdWho = (RadioButton)findViewById(R.id.rbtnStdWho);
                    RadioButton rbtnStdAsia = (RadioButton)findViewById(R.id.rbtnStdAsia);
                    RadioButton rbtnStdChn = (RadioButton)findViewById(R.id.rbtnStdChn);
                    if(rbtnStdWho.isChecked()){
                        if(bmi<18.5){
                            txtResult.setText("BMI:"+bmi.toString() + "体型:过轻");
                        }
                        else if(bmi<=24.9){
                            txtResult.setText("BMI:"+bmi.toString() + "体型:正常");
                        }
                        else if(bmi<=29.9){
                            txtResult.setText("BMI:"+bmi.toString() + "体型:过重");
                        }
                        else if(bmi<=34.9){
                            txtResult.setText("BMI:"+bmi.toString() + "体型:肥胖");
                        }
                        else{
                            txtResult.setText("BMI:"+bmi.toString() + "体型:非常肥胖");
                        }
    
                    }
                    else if(rbtnStdAsia.isChecked()){
                        if(bmi<18.5){
                            txtResult.setText("BMI:"+bmi.toString() + "体型:过轻");
                        }
                        else if(bmi<=23.9){
                            txtResult.setText("BMI:"+bmi.toString() + "体型:正常");
                        }
                        else if(bmi<=27){
                            txtResult.setText("BMI:"+bmi.toString() + "体型:过重");
                        }
                        else if(bmi<=32){
                            txtResult.setText("BMI:"+bmi.toString() + "体型:肥胖");
                        }
                        else{
                            txtResult.setText("BMI:"+bmi.toString() + "体型:非常肥胖");
                        }
    
                    }
                    else if(rbtnStdChn.isChecked()){
                        if(bmi<18.5){
                            txtResult.setText("BMI:"+bmi.toString() + "体型:过轻");
                        }
                        else if(bmi<=23.9){
                            txtResult.setText("BMI:"+bmi.toString() + "体型:正常");
                        }
                        else if(bmi<=27.9){
                            txtResult.setText("BMI:"+bmi.toString() + "体型:过重");
                        }
                        else{
                            txtResult.setText("BMI:"+bmi.toString() + "体型:非常肥胖");
                        }
    
                    }
                    else{
                        txtResult.setText("请选择BMI标准");
                    }
                }
            });
    
        }
    
    
    }
  • 相关阅读:
    Peewee中join三张及以上的表时只能获取一张表的数据
    Ubuntu18.04安装 NVIDIA显卡驱动+CUDA+cuDNN+Pytorch
    Kubernetes Deployment 并行重启Pod
    git config 配置用户名、邮箱
    Python __str__() 方法
    阅读-自律100天-SMART法则
    Jenkins 调用执行jmeter脚本,并直接生成html报告
    推荐一款开源的Diffy自动化测试框架(转)
    mysql binlog日志自动清理及手动删除
    大数据测试
  • 原文地址:https://www.cnblogs.com/wangdayang/p/14458589.html
Copyright © 2011-2022 走看看