AndroidStudio簡單實現(xiàn)BMI計算
本文實例為大家分享了AndroidStudio簡單實現(xiàn)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() 獲取編輯內容,再進行類型轉換
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Android學習教程之動態(tài)GridView控件使用(6)
這篇文章主要為大家詳細介紹了Android動態(tài)GridView控件的使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-11-11
Android 調用notifyDataSetChanged方法失敗解決辦法
這篇文章主要介紹了Android 調用notifyDataSetChanged方法失敗解決辦法的相關資料,需要的朋友可以參考下2017-07-07
AndroidStudio 使用過程中出現(xiàn)的異常(Gradle sync failed)處理辦法
本文主要介紹AndroidStudio 使用過程中出現(xiàn)的異常的解決辦法,這里幫大家舉例說明,如何處理出現(xiàn)這種問題,有需要的小伙伴可以參考下2016-09-09
Android UI設計與開發(fā)之ViewPager仿微信引導界面以及動畫效果
這篇文章主要為大家詳細介紹了Android UI設計與開發(fā)之ViewPager仿微信引導界面以及動畫效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08
Flutter 使用cached_image_network優(yōu)化圖片加載體驗
在 Flutter 中,cached_image_network 即提供了緩存網(wǎng)絡圖片功能,同時還提供了豐富的加載過程指示。本文就來看下cached_image_network的具體使用2021-05-05

