androidbmi体质指数计算器代码
时间: 2023-09-07 14:05:01 浏览: 78
### 回答1:
以下是一个简单的 Android BMI 体质指数计算器的代码示例:
MainActivity.java:
```java
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
EditText heightEditText, weightEditText;
Button calculateButton;
TextView resultTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
heightEditText = findViewById(R.id.heightEditText);
weightEditText = findViewById(R.id.weightEditText);
calculateButton = findViewById(R.id.calculateButton);
resultTextView = findViewById(R.id.resultTextView);
calculateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
double height = Double.parseDouble(heightEditText.getText().toString()) / 100;
double weight = Double.parseDouble(weightEditText.getText().toString());
double bmi = weight / (height * height);
if (bmi < 18.5) {
resultTextView.setText("你的 BMI 值为 " + bmi + ",属于偏瘦。");
} else if (bmi < 24) {
resultTextView.setText("你的 BMI 值为 " + bmi + ",属于正常体重。");
} else if (bmi < 28) {
resultTextView.setText("你的 BMI 值为 " + bmi + ",属于超重。");
} else {
resultTextView.setText("你的 BMI 值为 " + bmi + ",属于肥胖。");
}
}
});
}
}
```
activity_main.xml:
```xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/heightTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="身高(厘米):"
android:textSize="20sp"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"/>
<EditText
android:id="@+id/heightEditText"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:layout_toRightOf="@+id/heightTextView"
android:layout_marginTop="20dp"
android:layout_marginLeft="10dp"/>
<TextView
android:id="@+id/weightTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="体重(公斤):"
android:textSize="20sp"
android:layout_below="@+id/heightTextView"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"/>
<EditText
android:id="@+id/weightEditText"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:layout_toRightOf="@+id/weightTextView"
android:layout_below="@+id/heightEditText"
android:layout_marginTop="20dp"
android:layout_marginLeft="10dp"/>
<Button
android:id="@+id/calculateButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="计算"
android:textSize="20sp"
android:layout_below="@+id/weightTextView"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true"/>
<TextView
android:id="@+id/resultTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_below="@+id/calculateButton"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
```
这个计算器会根据用户输入的身高和体重计算出 BMI 值,并根据 BMI 值显示相应的结果。注意:这个代码示例仅用于学习和参考,实际项目中可能需要添加更多的输入验证和错误处理。
### 回答2:
下面是一个使用Java编写的Android BMI(体质指数)计算器代码示例:
```java
package com.example.bmicalculator;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private EditText heightInput;
private EditText weightInput;
private Button calculateButton;
private TextView resultText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
heightInput = findViewById(R.id.height_input);
weightInput = findViewById(R.id.weight_input);
calculateButton = findViewById(R.id.calculate_button);
resultText = findViewById(R.id.result_text);
calculateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
calculateBMI();
}
});
}
private void calculateBMI() {
String heightStr = heightInput.getText().toString();
String weightStr = weightInput.getText().toString();
if (!heightStr.isEmpty() && !weightStr.isEmpty()) {
float height = Float.parseFloat(heightStr);
float weight = Float.parseFloat(weightStr);
// 公式:BMI = 体重(kg)/ 身高(m)的平方
float bmi = weight / (height * height);
String result = String.format("您的BMI指数为%.2f", bmi);
resultText.setText(result);
} else {
resultText.setText("请填写身高和体重");
}
}
}
```
此代码使用Android的布局文件(activity_main.xml)来设置界面,包括两个EditText用于输入身高和体重,一个Button用于触发计算,以及一个TextView用于显示计算结果。
在MainActivity类中,我们在onCreate方法中初始化相关视图,并为计算按钮添加了一个点击事件监听器。当计算按钮被点击时,将调用calculateBMI方法进行计算。
calculateBMI方法从输入的身高和体重文本框获取数值,并根据BMI公式计算BMI值。最后,将结果以格式化字符串的形式显示在结果TextView上。
请注意,此代码仅为演示目的,并未添加错误处理或输入验证等功能。在实际应用中,您可能需要添加适当的错误处理和验证逻辑来确保用户输入的准确性和安全性。
### 回答3:
BMI(Body Mass Index)是一种衡量身体肥胖程度的指标,计算公式为BMI = 体重(kg)/身高^2(m^2)。下面是一段使用Java语言编写的Android代码,实现BMI体质指数计算器的功能:
```java
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private EditText etWeight;
private EditText etHeight;
private Button btnCalculate;
private TextView tvResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etWeight = findViewById(R.id.et_weight);
etHeight = findViewById(R.id.et_height);
btnCalculate = findViewById(R.id.btn_calculate);
tvResult = findViewById(R.id.tv_result);
btnCalculate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 获取输入的体重和身高
double weight = Double.parseDouble(etWeight.getText().toString());
double height = Double.parseDouble(etHeight.getText().toString());
// 计算BMI
double bmi = weight / (height * height);
// 根据BMI值判断体质状况
String result;
if (bmi < 18.5) {
result = "体重过轻";
} else if (bmi < 24) {
result = "体重正常";
} else if (bmi < 28) {
result = "超重";
} else {
result = "肥胖";
}
// 将结果显示在TextView上
tvResult.setText("BMI指数:" + bmi + "\n" + result);
}
});
}
}
```
以上代码中,首先在布局文件中定义了两个EditText用于输入体重和身高,一个Button用于触发计算,一个TextView用于显示计算结果。在Activity的onCreate方法中,通过findViewById获取到对应的控件,并添加点击事件监听器。点击按钮后,获取输入的体重和身高,并根据BMI值判断体质状况,然后将结果显示在TextView上。