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)換,可以采用 對(duì)象.getText().toString() 獲取編輯內(nèi)容,再進(jìn)行類型轉(zhuǎn)換
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android調(diào)用系統(tǒng)的發(fā)郵件功能的小例子
這篇文章介紹了Android調(diào)用系統(tǒng)的發(fā)郵件功能的小例子,有需要的朋友可以參考一下2013-08-08Android條紋進(jìn)度條的實(shí)現(xiàn)(調(diào)整view寬度仿進(jìn)度條)
這篇文章主要給大家介紹了關(guān)于Android實(shí)現(xiàn)條紋進(jìn)度條的方法,主要是通過調(diào)整view寬度仿進(jìn)度條,文中通過示例代碼介紹的非常詳細(xì),對(duì)各位Android開發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧2018-09-09在Android中動(dòng)態(tài)添加Panel框架的實(shí)現(xiàn)代碼
項(xiàng)目經(jīng)常會(huì)有這種需求,就是想動(dòng)態(tài)的在某個(gè)界面上添加一個(gè)Panel。比如,有一個(gè)按鈕,點(diǎn)擊后會(huì)顯示下拉菜單式的界面。這種需求,就屬于動(dòng)態(tài)添加一個(gè)Panel。需求多了,就要研究是否可以抽象出通用的框架代碼,以方便開發(fā),所以就有了以下內(nèi)容2013-05-05ImageView點(diǎn)擊可變暗的實(shí)例代碼(android代碼技巧)
本文給大家分享一段實(shí)例代碼給大家介紹ImageView點(diǎn)擊可變暗的實(shí)例代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的的朋友參考下吧2017-02-02Android編程實(shí)現(xiàn)簡單文件瀏覽器功能
這篇文章主要介紹了Android編程實(shí)現(xiàn)簡單文件瀏覽器功能,結(jié)合實(shí)例形式分析了Android文件管理器的布局、文件與目錄的遍歷、權(quán)限控制等相關(guān)操作技巧,需要的朋友可以參考下2018-01-01微信支付僅能成功調(diào)用一次問題的解決方法(Android)
這篇文章主要介紹了微信支付僅能成功調(diào)用一次問題的解決方法,感興趣的小伙伴們可以參考一下2016-08-08Android實(shí)戰(zhàn)RecyclerView頭部尾部添加方法示例
本篇文章主要介紹了Android實(shí)戰(zhàn)RecyclerView頭部尾部添加方法示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-11-11Android檢查網(wǎng)絡(luò)狀態(tài)工具類詳解
這篇文章主要為大家詳細(xì)介紹了Android檢查網(wǎng)絡(luò)狀態(tài)工具類,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04