BMI计算器(Android)

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"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <TextView
        android:textSize="36sp"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="BMI计算器"
        android:layout_marginTop="48dp"
        android:layout_marginBottom="16dp"
        />

    <EditText
        android:id="@+id/weight"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:drawablePadding="8dp"
        android:hint="输入体重 单位kg"
        android:imeOptions="actionNext"
        android:inputType="text"/>

    <EditText
        android:id="@+id/height"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:drawablePadding="8dp"
        android:hint="输入身高 单位m"
        android:imeOptions="actionNext"
        android:inputType="text"
        />


    <RadioGroup
    android:id="@+id/who"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    >

        <TextView
        android:id="@+id/xb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="性别:"
        />

    <RadioButton
        android:id="@+id/men"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="男" />

    <RadioButton
        android:id="@+id/women"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="女" />
    </RadioGroup>
        <Button
            android:id="@+id/bt_js"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="计算"
            android:onClick="ButtonClick"
            tools:ignore="OnClick" />
    <TextView
        android:id="@+id/txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="结论:"
        />

</LinearLayout>

MainActivity.java代码:

package com.example.bmi;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
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);
    }
    public  void  ButtonClick(View v){
        EditText editHeight = (EditText)findViewById(R.id.height);
        EditText editWeidth = (EditText)findViewById(R.id.weight);
        TextView txtResault = (TextView)findViewById(R.id.txt);
        //获取编辑框内容,由于是字符串类型,需要转换为可计算类型
        Double height = Double.parseDouble(editHeight.getText().toString());
        Double weight = Double.parseDouble(editWeidth.getText().toString());
        //设置判断语句
        Double bmi = weight / (height*height);

        RadioButton rb1 = (RadioButton)findViewById(R.id.men);
        RadioButton rb2 = (RadioButton)findViewById(R.id.women);

        //判断控件按钮是否被选中 isChecked()
        if(rb1.isChecked()){
            if(bmi<20){
                txtResault.setText("BMI"+bmi.toString()+",您的体重偏轻");
            }
            else  if(bmi<=25){
                txtResault.setText("BMI"+bmi.toString()+",您的体重正常");
            }
            else if(bmi<=27){
                txtResault.setText("BMI"+bmi.toString()+",您的体重偏重");
            }
            else if (bmi<=30){
                txtResault.setText("BMI"+bmi.toString()+",您的体重轻度肥胖!!!");
            }
            else if (bmi<=35){
                txtResault.setText("BMI"+bmi.toString()+",您的体重中度肥胖!!!!");
            }
            else {
                txtResault.setText("BMI"+bmi.toString()+",您的体重严重肥胖!!!!!!!");
            }
        }
        else if(rb2.isChecked()){
            if(bmi<19){
                txtResault.setText("BMI"+bmi.toString()+",您的体重偏轻");
            }
            else  if(bmi<=24){
                txtResault.setText("BMI"+bmi.toString()+",您的体重正常");
            }
            else if(bmi<=26){
                txtResault.setText("BMI"+bmi.toString()+",您的体重偏重");
            }
            else if (bmi<=29){
                txtResault.setText("BMI"+bmi.toString()+",您的体重肥胖!!!");
            }
            else if (bmi<=34){
                txtResault.setText("BMI"+bmi.toString()+",您的体重过于肥胖!!!!");
            }
            else{
                txtResault.setText("BMI"+bmi.toString()+",您的体重严重肥胖!!!!!!!");
            }

        }
        else {
            txtResault.setText("请选择");
        }
    }
}

Android BMI计算器代码相对简单,主要利用Android开发中的布局和事件处理等组件。以下是一个简单的示例代码: 1. 定义XML布局文件,放置计算器的界面元素。可以使用LinearLayout或RelativeLayout等布局方式,如下所示: ```xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="10dp"> <EditText android:id="@+id/edittextHeight" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:hint="身高(米)" android:inputType="numberDecimal" /> <EditText android:id="@+id/edittextWeight" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/edittextHeight" android:layout_marginTop="20dp" android:hint="体重(公斤)" android:inputType="numberDecimal" /> <Button android:id="@+id/buttonCalculate" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/edittextWeight" android:layout_marginTop="20dp" android:text="计算BMI" /> <TextView android:id="@+id/textviewResult" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/buttonCalculate" android:layout_marginTop="20dp" android:textSize="18sp" /> </RelativeLayout> ``` 2. 在MainActivity中实现事件处理和计算BMI值。可以定义一个方法calculateBMI(),根据身高和体重的数据计算BMI值,然后显示在界面上。如下所示: ```java public class MainActivity extends AppCompatActivity { private EditText mEdittextHeight; private EditText mEdittextWeight; private Button mButtonCalculate; private TextView mTextviewResult; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mEdittextHeight = (EditText) findViewById(R.id.edittextHeight); mEdittextWeight = (EditText) findViewById(R.id.edittextWeight); mButtonCalculate = (Button) findViewById(R.id.buttonCalculate); mTextviewResult = (TextView) findViewById(R.id.textviewResult); mButtonCalculate.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { calculateBMI(); } }); } private void calculateBMI() { String strHeight = mEdittextHeight.getText().toString(); String strWeight = mEdittextWeight.getText().toString(); if (TextUtils.isEmpty(strHeight) || TextUtils.isEmpty(strWeight)) { mTextviewResult.setText("请输入身高和体重"); return; } float height = Float.parseFloat(strHeight); float weight = Float.parseFloat(strWeight); if (height <= 0 || weight <= 0) { mTextviewResult.setText("身高和体重必须大于0"); return; } float bmi = weight / (height * height); String strResult = "你的BMI指数为:" + String.format("%.2f", bmi); if (bmi < 18.5) { strResult += "\n体重过轻"; } else if (bmi < 24.9) { strResult += "\n体重正常"; } else if (bmi < 29.9) { strResult += "\n体重过重"; } else { strResult += "\n肥胖"; } mTextviewResult.setText(strResult); } } ``` 这样,就可以完成一个简单的Android BMI计算器。用户输入身高和体重数据后,点击计算按钮即可得到BMI指数值和对应的体重状况提示。当然,代码还可以进行更多的优化和扩展,例如添加数据校验、保存历史记录等功能。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值