安卓android BMI体质指数测试项目

这是一个简单的Android应用程序,用于计算BMI指数。用户输入身高、体重,选择性别后,应用会根据输入计算BMI并显示相应的体重状况。点击'重置'按钮可以清除所有输入,'计算'按钮则触发BMI计算。性别选择通过RadioButton实现,结果通过Toast短暂显示。
摘要由CSDN通过智能技术生成

一、 界面布局:

(1)、 主界面:

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">
    //用户
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginLeft="50dp"
        android:layout_marginTop="50dp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="用户"
            android:textSize="40dp"/>
        <EditText
            android:id="@+id/ID"
            android:layout_width="200dp"
            android:layout_height="match_parent"
            />
        ></LinearLayout>
    //密码
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginLeft="50dp"
        android:layout_marginTop="50dp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="密码"
            android:textSize="40dp"/>
        <EditText
            android:id="@+id/PASS"
            android:layout_width="200dp"
            android:layout_height="match_parent"
            android:inputType="numberPassword"
            />
        ></LinearLayout>
    //身高
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginLeft="50dp"
        android:layout_marginTop="50dp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="身高"
            android:textSize="40dp"/>
        <EditText
            android:id="@+id/H"
            android:text="m"
            android:layout_width="200dp"
            android:layout_height="match_parent"
            />
        ></LinearLayout>
    //体重
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginLeft="50dp"
        android:layout_marginTop="50dp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="体重"
            android:textSize="40dp"/>
        <EditText
            android:id="@+id/W"
            android:text="kg"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            />
        ></LinearLayout>
    //男女选项
    <RadioGroup
        android:id="@+id/sex"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginLeft="90dp"
        android:layout_marginTop="15dp">
        <RadioButton
            android:id="@+id/man"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""
            android:textSize="30dp"
            />
        <RadioButton
            android:id="@+id/woman"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="70dp"
            android:text=""
            android:textSize="30dp"
            />
        ></RadioGroup>
    //按钮
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginLeft="90dp"
        android:layout_marginTop="20dp">
        <Button
            android:id="@+id/b1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="计算"
            android:textSize="30dp"
            />
        <Button
            android:id="@+id/b2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="50dp"
            android:text="重置"
            android:textSize="30dp"
            />
        ></LinearLayout>

</LinearLayout>

MainActivity.java源代码:

package com.example.bim;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
import android.view.View;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
        private Button b1,b2;
        private RadioButton man,woman;
        private EditText heightText,weightText,ID,PASS;
        private TextView resText;
    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //ID = findViewById(R.id.ID);
        //PASS = findViewById(R.id.PASS);密码和用户名没有用到
        //以下为变量与控件的绑定
        b1 = (Button) findViewById(R.id.b1);
        heightText = (EditText) findViewById(R.id.H);
        weightText = (EditText) findViewById(R.id.W);
        b2 = (Button) findViewById(R.id.b2);
        man = findViewById(R.id.man);
        woman = findViewById(R.id.woman);

        //button2的点击响应,作用为清空所有输入
        b2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ID.setText("");//清空实质为把所有输入换成空
                PASS.setText("");
                heightText.setText("");
                weightText.setText("");

            }
        });
        //button2的点击事件响应,计算BMI的值,结果用Toast显示
        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //得到身高体重
                String height = heightText.getText().toString();
                String weight = weightText.getText().toString();
                double result = 0, heightNum = 0, weightNum = 0;
                if(!height.isEmpty()&&!weight.isEmpty()) {
                    heightNum = Double.parseDouble(height);
                    weightNum = Double.parseDouble(weight);
                    result = weightNum / (heightNum*heightNum);
                }
                if(man.isChecked()){//如果选择的是男性
                    if(result <= 19)
                    {
                        Toast.makeText(MainActivity.this,"体重偏低",Toast.LENGTH_SHORT).show();
                    }
                    else if(result <= 25 && result > 19)
                    {
                        Toast.makeText(MainActivity.this,"健康体重",Toast.LENGTH_SHORT).show();
                    }
                    else if(result <= 30 && result > 25)
                    {
                        Toast.makeText(MainActivity.this,"超重",Toast.LENGTH_SHORT).show();
                    }
                    else if(result < 39 && result > 30)
                    {
                        Toast.makeText(MainActivity.this,"严重超重",Toast.LENGTH_SHORT).show();
                    }
                    else {
                        Toast.makeText(MainActivity.this,"极度超重",Toast.LENGTH_SHORT).show();
                    }

                }
                else//选择的是女性
                {
                    if (result <= 18) {
                        Toast.makeText(MainActivity.this, "体重偏低", Toast.LENGTH_SHORT).show();
                    } else if (result <= 24 && result > 18) {
                        Toast.makeText(MainActivity.this, "健康体重", Toast.LENGTH_SHORT).show();
                    } else if (result <= 29 && result > 24) {
                        Toast.makeText(MainActivity.this, "超重", Toast.LENGTH_SHORT).show();
                    } else if (result < 38 && result > 29) {
                        Toast.makeText(MainActivity.this, "严重超重", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(MainActivity.this, "极度超重", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
   }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值