AndroidStudio簡單實(shí)現(xiàn)BMI計(jì)算
本文實(shí)例為大家分享了AndroidStudio簡單實(shí)現(xiàn)BMI計(jì)算的具體代碼,供大家參考,具體內(nèi)容如下
xml代碼
```xml
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="30dp"
android:text="BMI計(jì)算器"
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="結(jié)論"
/>
<Button
android:id="@+id/btn_count"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/rdgp"
android:text="計(jì)算"
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標(biāo)準(zhǔn)" />
<RadioButton
android:id="@+id/rbtn_asia"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="亞洲標(biāo)準(zhǔn)" />
<RadioButton
android:id="@+id/rbtn_chn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="中國標(biāo)準(zhǔn)" />
</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);
//獲取編輯框內(nèi)容,由于是字符串類型,需要轉(zhuǎn)換為可計(jì)算類型
Double height = Double.parseDouble(editHeight.getText().toString());
Double weight = Double.parseDouble(editWeidth.getText().toString());
//設(shè)置判斷語句
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()+",您的體重嚴(yán)重肥胖!!!!!!!");
}
}
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()+",您的體重嚴(yán)重肥胖!!!!!!!");
}
}
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標(biāo)準(zhǔn)");
}
}

總結(jié):
1.判斷選擇按鈕時(shí),可以采用isChecked進(jìn)行判斷;
2.類型轉(zhuǎn)換,可以采用 對象.getText().toString() 獲取編輯內(nèi)容,再進(jìn)行類型轉(zhuǎn)換
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android學(xué)習(xí)教程之動(dòng)態(tài)GridView控件使用(6)
這篇文章主要為大家詳細(xì)介紹了Android動(dòng)態(tài)GridView控件的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11
Android Studio實(shí)現(xiàn)井字游戲
這篇文章主要為大家詳細(xì)介紹了Android Studio實(shí)現(xiàn)井字游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
Android 調(diào)用notifyDataSetChanged方法失敗解決辦法
這篇文章主要介紹了Android 調(diào)用notifyDataSetChanged方法失敗解決辦法的相關(guān)資料,需要的朋友可以參考下2017-07-07
簡單實(shí)現(xiàn)Android應(yīng)用的啟動(dòng)頁
這篇文章主要介紹了簡單實(shí)現(xiàn)Android應(yīng)用的啟動(dòng)頁,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-09-09
AndroidStudio 使用過程中出現(xiàn)的異常(Gradle sync failed)處理辦法
本文主要介紹AndroidStudio 使用過程中出現(xiàn)的異常的解決辦法,這里幫大家舉例說明,如何處理出現(xiàn)這種問題,有需要的小伙伴可以參考下2016-09-09
Android UI設(shè)計(jì)與開發(fā)之ViewPager仿微信引導(dǎo)界面以及動(dòng)畫效果
這篇文章主要為大家詳細(xì)介紹了Android UI設(shè)計(jì)與開發(fā)之ViewPager仿微信引導(dǎo)界面以及動(dòng)畫效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
Flutter 使用cached_image_network優(yōu)化圖片加載體驗(yàn)
在 Flutter 中,cached_image_network 即提供了緩存網(wǎng)絡(luò)圖片功能,同時(shí)還提供了豐富的加載過程指示。本文就來看下cached_image_network的具體使用2021-05-05
Android實(shí)現(xiàn)View滑動(dòng)效果的6種方法
這篇文章主要介紹了Android實(shí)現(xiàn)View滑動(dòng)的6種方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-03-03

