AndroidStudio簡單實現(xiàn)BMI計算
本文實例為大家分享了AndroidStudio簡單實現(xiàn)BMI計算的具體代碼,供大家參考,具體內(nèi)容如下
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="結(jié)論"
/>
<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);
//獲取編輯框內(nèi)容,由于是字符串類型,需要轉(zhuǎn)換為可計算類型
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標準");
}
}

總結(jié):
1.判斷選擇按鈕時,可以采用isChecked進行判斷;
2.類型轉(zhuǎn)換,可以采用 對象.getText().toString() 獲取編輯內(nèi)容,再進行類型轉(zhuǎn)換
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android調(diào)用系統(tǒng)的發(fā)郵件功能的小例子
這篇文章介紹了Android調(diào)用系統(tǒng)的發(fā)郵件功能的小例子,有需要的朋友可以參考一下2013-08-08
Android條紋進度條的實現(xiàn)(調(diào)整view寬度仿進度條)
這篇文章主要給大家介紹了關(guān)于Android實現(xiàn)條紋進度條的方法,主要是通過調(diào)整view寬度仿進度條,文中通過示例代碼介紹的非常詳細,對各位Android開發(fā)者們具有一定的參考學習價值,需要的朋友們下面來一起看看吧2018-09-09
在Android中動態(tài)添加Panel框架的實現(xiàn)代碼
項目經(jīng)常會有這種需求,就是想動態(tài)的在某個界面上添加一個Panel。比如,有一個按鈕,點擊后會顯示下拉菜單式的界面。這種需求,就屬于動態(tài)添加一個Panel。需求多了,就要研究是否可以抽象出通用的框架代碼,以方便開發(fā),所以就有了以下內(nèi)容2013-05-05
ImageView點擊可變暗的實例代碼(android代碼技巧)
本文給大家分享一段實例代碼給大家介紹ImageView點擊可變暗的實例代碼,非常不錯,具有參考借鑒價值,需要的的朋友參考下吧2017-02-02
微信支付僅能成功調(diào)用一次問題的解決方法(Android)
這篇文章主要介紹了微信支付僅能成功調(diào)用一次問題的解決方法,感興趣的小伙伴們可以參考一下2016-08-08
Android實戰(zhàn)RecyclerView頭部尾部添加方法示例
本篇文章主要介紹了Android實戰(zhàn)RecyclerView頭部尾部添加方法示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11

