📜  如何在 Android Studio 中构建体重指数计算器?

📅  最后修改于: 2022-05-13 01:55:51.975000             🧑  作者: Mango

如何在 Android Studio 中构建体重指数计算器?

体重指数 (BMI) 计算器可用于根据身高和体重计算 BMI 值。对于大多数人来说,BMI 是一个相当可靠的体脂指标。

公式:

方法:BMI 是根据个人的体重和身高计算得出的数字。为了找出 BMI,我们将从用户那里获取输入(包括身高和体重),这些输入将存储在身高和体重变量中以供进一步计算。计算过程很简单,我们只需将体重(公斤)除以身高的平方即可。现在根据计算出的 BMI,它将执行相应的 if-else 语句。我们还检查用户是否在没有输入输入的情况下按下提交按钮,在这种情况下,我们将显示一条 Toast 消息,说“请输入有效的身高和体重”。在本文中,我们将使用 Kotlin和 XML 在 Android Studio 中构建体重指数计算器。

分步实施

第 1 步:创建一个新项目



要在 Android Studio 中创建新项目,请参阅如何在 Android Studio 中创建/启动新项目。请注意,选择Kotlin作为编程语言。

第 2 步:使用 build.gradle(Module) 文件

您需要像这样在 app build.gradle 模块中应用插件 kotlin-android-extensions

步骤 3:使用 activity_main.xml 文件

导航到app > res > layout > activity_main.xml并将以下代码添加到该文件中。下面是activity_main.xml文件的代码。

XML


  
    
  
    
  
    
  
    
  
    
  
    


Kotlin
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
  
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
  
        calculate_btn.setOnClickListener {
  
            // Check if the height EditText and Weight EditText are not empty
            if (etHeight.text.isNotEmpty() && etWeight.text.isNotEmpty()) {
                val height = (etHeight.text.toString()).toInt()
                val weight = (etWeight.text.toString()).toInt()
  
                // calculateBMI will return BMI
                val BMI = calculateBMI(height, weight)
  
                bmi.text = BMI.toString()
                bmi.visibility = View.VISIBLE
  
                // update the status text as per the bmi conditions
                if (BMI < 18.5) {
                    status.text = "Under Weight"
                } else if (BMI >= 18.5 && BMI < 24.9) {
                    status.text = "Healthy"
                } else if (BMI >= 24.9 && BMI < 30) {
                    status.text = "Overweight"
                } else if (BMI >= 30) {
                    status.text = "Suffering from Obesity"
                }
  
                bmi_tv.visibility = View.VISIBLE
                status.visibility = View.VISIBLE
  
                ReCalculate.visibility = View.VISIBLE
                calculate_btn.visibility = View.GONE
                  
            }
  
            // when either Weight EditText or 
            // height EditText have null value
            // we will display toast.
            else {
                Toast.makeText(this, "please enter the valid height and weight", Toast.LENGTH_SHORT).show()
            }
        }
          
        ReCalculate.setOnClickListener {
            ResetEverything()
        }
  
    }
  
    // Function to reset all Text and EditText fields.
    private fun ResetEverything() {
          
        calculate_btn.visibility = View.VISIBLE
        ReCalculate.visibility = View.GONE
  
        etHeight.text.clear()
        etWeight.text.clear()
        status.text = " "
        bmi.text = " "
        bmi_tv.visibility = View.GONE
    }
  
    // Function for calculating BMI
    private fun calculateBMI(height: Int, weight: Int): Float {
          
        val Height_in_metre = height.toFloat() / 100
        val BMI = weight.toFloat() / (Height_in_metre * Height_in_metre)
  
        return BMI
    }
}


写了这么多代码后,我们的 UI 看起来是这样的:

步骤 4:使用 MainActivity.kt 文件

转到MainActivity.kt文件并参考以下代码。下面是MainActivity.kt文件的代码。代码中添加了注释以更详细地理解代码。

科特林

import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
  
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
  
        calculate_btn.setOnClickListener {
  
            // Check if the height EditText and Weight EditText are not empty
            if (etHeight.text.isNotEmpty() && etWeight.text.isNotEmpty()) {
                val height = (etHeight.text.toString()).toInt()
                val weight = (etWeight.text.toString()).toInt()
  
                // calculateBMI will return BMI
                val BMI = calculateBMI(height, weight)
  
                bmi.text = BMI.toString()
                bmi.visibility = View.VISIBLE
  
                // update the status text as per the bmi conditions
                if (BMI < 18.5) {
                    status.text = "Under Weight"
                } else if (BMI >= 18.5 && BMI < 24.9) {
                    status.text = "Healthy"
                } else if (BMI >= 24.9 && BMI < 30) {
                    status.text = "Overweight"
                } else if (BMI >= 30) {
                    status.text = "Suffering from Obesity"
                }
  
                bmi_tv.visibility = View.VISIBLE
                status.visibility = View.VISIBLE
  
                ReCalculate.visibility = View.VISIBLE
                calculate_btn.visibility = View.GONE
                  
            }
  
            // when either Weight EditText or 
            // height EditText have null value
            // we will display toast.
            else {
                Toast.makeText(this, "please enter the valid height and weight", Toast.LENGTH_SHORT).show()
            }
        }
          
        ReCalculate.setOnClickListener {
            ResetEverything()
        }
  
    }
  
    // Function to reset all Text and EditText fields.
    private fun ResetEverything() {
          
        calculate_btn.visibility = View.VISIBLE
        ReCalculate.visibility = View.GONE
  
        etHeight.text.clear()
        etWeight.text.clear()
        status.text = " "
        bmi.text = " "
        bmi_tv.visibility = View.GONE
    }
  
    // Function for calculating BMI
    private fun calculateBMI(height: Int, weight: Int): Float {
          
        val Height_in_metre = height.toFloat() / 100
        val BMI = weight.toFloat() / (Height_in_metre * Height_in_metre)
  
        return BMI
    }
}

输出:

想要一个更快节奏和更具竞争力的环境来学习 Android 的基础知识吗?
单击此处前往由我们的专家精心策划的指南,旨在让您立即做好行业准备!