簡單的BMI計算器


1.把需要用的資源聲明出來,有的用findViewById找出來,沒有的new出來,比如按鈕的監聽器。

2.因為這個程序做的是數值計算,默認的往里輸入的會默認為edit型,從資源取出並計算時要轉為double型,先取后轉Double.parseDouble(filed_height.getText().toString())

3.計算出的數值要返回在界面上顯示,要格式化一下,setText表示插入到頁面的值。在實例化時用0.00表示輸出的形式,還有##.##的,這個是首末位不包含0的。

DecimalFormat df=new DecimalFormat("0.00");
result.setText("你的BMI值為"+df.format(bmi));

4.對計算出的結果進行判斷,if和else即可。

 

布局 main_bmi.xml

 1 <LinearLayout
 2     xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:tools="http://schemas.android.com/tools"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:orientation="vertical"
 7     >
 8 
 9     <TextView
10         android:layout_width="fill_parent"
11         android:layout_height="wrap_content"
12         android:text="@string/height" /> <!--講字符串從本xml抽離,添加到另一個索引的strings.xml里-->
13     <EditText 
14         android:id="@+id/height"
15         android:layout_width="fill_parent"
16         android:layout_height="wrap_content"
17         android:inputType="numberDecimal"<!--輸入類型為數字小數型-->
18         />
19     <TextView
20         android:layout_width="wrap_content"
21         android:layout_height="wrap_content"
22         android:text="@string/weight" 
23         />
24     <EditText 
25         android:id="@+id/weight"
26         android:layout_width="fill_parent"
27         android:layout_height="wrap_content"
28         android:inputType="numberDecimal"
29         />
30     <Button 
31         android:id="@+id/submit"
32         android:layout_width="fill_parent"
33         android:layout_height="wrap_content"
34         android:text="@string/bmi_btn" 
35         />
36     <TextView 
37         android:id="@+id/result"
38         android:layout_width="fill_parent"
39         android:layout_height="wrap_content"
40         android:text="@string/bmi_result"
41         />
42     <TextView 
43         android:id="@+id/suggest"
44         android:layout_width="fill_parent"
45         android:layout_height="wrap_content"
46         android:text=""
47         />
48 
49 </LinearLayout>

strings.xml 默認的

1  <string name="app_name">BMI</string>
2     <string name="hello_world">Hello world!</string>
3     <string name="action_settings">Settings</string>
4     <string name="height">身高</string>
5     <string name="weight">體重</string>
6     <string name="bmi_btn">計算BMI值</string>
7     <string name="bmi_result">你的BMI值是</string>

advince.xml 算出后bmi值給的建議,這里新建一個

1  <string name="advice_light">你該多吃點</string>
2     <string name="advice_average">體型很不錯喔</string>
3     <string name="advice_heavy">你該節食了</string>

bmi.java

 1 public class BMI extends ActionBarActivity {
 2     private Button btn;
 3     private OnClickListener the_bmi;
 4     private EditText filed_height;
 5     private EditText filed_weight;
 6     private TextView result;
 7     private TextView suggest;
 8     
 9     private double height;
10     private double weight;
11     private double bmi;
12     @Override
13     protected void onCreate(Bundle savedInstanceState) {
14         super.onCreate(savedInstanceState);
15         setContentView(R.layout.activity_bmi);
16         
17         btn=(Button) findViewById(R.id.submit);
18         btn.setOnClickListener(new OnClickListener() {
19             
20             @Override
21             public void onClick(View v) {
22                 // TODO Auto-generated method stub
23                 filed_height=(EditText) findViewById(R.id.height);
24                 filed_weight=(EditText) findViewById(R.id.weight);
25                 height=Double.parseDouble(filed_height.getText().toString())/100;
26                 weight=Double.parseDouble(filed_weight.getText().toString());
27                 bmi=weight/(height*height);
28                 
29                 
30                 DecimalFormat df=new DecimalFormat("0.00");
31                 result=(TextView) findViewById(R.id.result);
32                 result.setText("你的BMI值為"+df.format(bmi));
33                 
34                 suggest=(TextView) findViewById(R.id.suggest);
35                 if(bmi>25)
36                     suggest.setText(R.string.advice_heavy);
37                 else if(bmi<20)
38                     suggest.setText(R.string.advice_light);
39                 else
40                     suggest.setText(R.string.advice_average);
41             }
42         });
43         
44         
45         
46         
47     }

 

介個樣紫

 

剛開始再給button定義監聽器是先聲明一個監聽器,在作為參數給button的,但不好使啊,只能在set里寫了,這樣看好亂,為什么呢......?

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM