AndroidStudio简单实现BMI计算
作者:西_窗 时间:2022-11-24 03:14:50
本文实例为大家分享了AndroidStudio简单实现BMI计算的具体代码,供大家参考,具体内容如下
xml代码
```xml
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="30dp"
android:text="BMI计算器"
android:textSize="25dp"/>
<EditText
android:id="@+id/height"
android:layout_width="match_parent"
android:layout_below="@id/textView"
android:layout_height="50dp"
android:hint="身高(米)"
android:layout_marginLeft="20dp"
/>
<EditText
android:id="@+id/weight"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="@id/height"
android:hint="体重(公斤)"
android:layout_marginLeft="20dp"
/>
<TextView
android:id="@+id/txt"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="@id/weight"
android:text="结论"
/>
<Button
android:id="@+id/btn_count"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/rdgp"
android:text="计算"
android:onClick="ButtonClick"
/>
<RadioGroup
android:id="@+id/rdgp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="@id/txt">
<RadioButton
android:id="@+id/rbtn_who"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="WHO标准" />
<RadioButton
android:id="@+id/rbtn_asia"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="亚洲标准" />
<RadioButton
android:id="@+id/rbtn_chn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="中国标准" />
</RadioGroup>
java代码
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);
/*
BMI在18.5-24之间是正常的。
BMI低于18.5考虑为体重过轻;
24-27之间为超重;
超过27以上为肥胖。
*/
RadioButton rdbtn_1 = (RadioButton)findViewById(R.id.rbtn_who);
RadioButton rdbtn_2 = (RadioButton)findViewById(R.id.rbtn_asia);
RadioButton rdbtn_3 = (RadioButton)findViewById(R.id.rbtn_chn);
//判断控件按钮是否被选中 isChecked()
if(rdbtn_1.isChecked()){
if(bmi<18.5){
txtResault.setText("BMI"+bmi.toString()+",您的体重偏轻");
}
else if(bmi<=24.9){
txtResault.setText("BMI"+bmi.toString()+",您的体重正常");
}
else if(bmi<=29.9){
txtResault.setText("BMI"+bmi.toString()+",您的体重偏重");
}
else if (bmi<=34.9){
txtResault.setText("BMI"+bmi.toString()+",您的体重肥胖!!!");
}
else if (bmi<=39.9){
txtResault.setText("BMI"+bmi.toString()+",您的体重过于肥胖!!!!");
}
else {
txtResault.setText("BMI"+bmi.toString()+",您的体重严重肥胖!!!!!!!");
}
}
else if(rdbtn_2.isChecked()){
if(bmi<18.5){
txtResault.setText("BMI"+bmi.toString()+",您的体重偏轻");
}
else if(bmi<=22.9){
txtResault.setText("BMI"+bmi.toString()+",您的体重正常");
}
else if(bmi<=24.9){
txtResault.setText("BMI"+bmi.toString()+",您的体重偏重");
}
else if (bmi<=29.9){
txtResault.setText("BMI"+bmi.toString()+",您的体重肥胖!!!");
}
else if (bmi<=40){
txtResault.setText("BMI"+bmi.toString()+",您的体重过于肥胖!!!!");
}
else{
txtResault.setText("BMI"+bmi.toString()+",您的体重严重肥胖!!!!!!!");
}
}
else if (rdbtn_3.isChecked()){
if(bmi<18.5){
txtResault.setText("BMI"+bmi.toString()+",您的体重偏轻");
}
else if(bmi<=23.9){
txtResault.setText("BMI"+bmi.toString()+",您的体重正常");
}
else if(bmi<=27.9){
txtResault.setText("BMI"+bmi.toString()+",您的体重偏重");
}
else {
txtResault.setText("BMI"+bmi.toString()+",您的体重肥胖");
}
}
else {
txtResault.setText("请选择BMI标准");
}
}
总结:
1.判断选择按钮时,可以采用isChecked进行判断;
2.类型转换,可以采用 对象.getText().toString() 获取编辑内容,再进行类型转换
来源:https://blog.csdn.net/qq_46485804/article/details/115964012
标签:AndroidStudio,BMI,计算
0
投稿
猜你喜欢
Springboot创建子父工程过程图解
2022-09-20 06:06:26
Idea springboot springCloud热加载热调试两种常用方式
2021-06-11 22:20:06
Kotlin如何直接使用控件ID原理详析
2022-05-01 16:45:42
IDEA2020.1使用LeetCode插件运行并调试本地样例的方法详解
2022-02-28 09:44:47
Android App中ListView仿QQ实现滑动删除效果的要点解析
2022-11-22 03:05:57
SpringBoot实现分页功能
2021-11-07 12:33:16
Android应用UI开发中Fragment的常见用法小结
2021-06-16 19:35:54
Java中如何计算一段程序的运行时间
2022-12-17 10:52:06
SpringBoot文件上传控制及Java 获取和判断文件头信息
2021-12-10 16:47:20
Android handle-message的发送与处理案例详解
2023-07-21 09:28:13
java身份证验证代码实现
2023-12-09 16:10:50
C#遍历List并删除某个元素的方法
2023-03-22 10:22:08
Android实现声音采集回声与回声消除
2022-09-28 12:13:17
浅析C# 函数的传值与传址
2023-11-22 04:46:57
C#策略模式(Strategy Pattern)实例教程
2022-11-29 07:35:07
关于apollo和Spring集成@Value注解通用解析
2022-03-30 23:06:03
Android百度地图应用之基本地图功能实现
2022-11-20 07:01:41
c#字符串编码编码(encoding)使用方法示例
2022-10-04 07:24:58
C#实现JSON解析器MojoUnityJson功能(简单且高效)
2023-12-02 16:44:50
基于springboot搭建的web系统架构的方法步骤
2023-11-21 13:33:51