下面是一個簡單的計算標準體重的實例,選擇自己的性別,再輸入自己的身高,點擊Button就能在Toast顯示自己的標準體重,看看自己的體重有沒有符合標準哦。
計算標準體重的方法:
男性:(身高cm-80)×70﹪=標準體重 女性:(身高cm-70)×60﹪=標準體重
BMIActivity.java
1. package com.lingdududu.bmi;
2.
3. import java.text.DecimalFormat;
4. import java.text.NumberFormat;
5. import android.app.Activity;
6. import android.os.Bundle;
7. import android.view.View;
8. import android.view.View.OnClickListener;
9. import android.widget.Button;
10. import android.widget.EditText;
11. import android.widget.RadioButton;
12. import android.widget.Toast;
13. /*
14. * @author lingdududu * 該程序的功能是用戶選擇自己的性別和輸入自己的身高,然後點擊按鈕,就能在Toast顯示出自己的標準體重
15. */
16. public class BMIActivity extends Activity {
17. /** Called when the activity is first created. */
18. private Button countButton;
19. private EditText heighText;
20. private RadioButton maleBtn, femaleBtn;
21. String sex = "";
22. double height;
23. @Override
24. public void onCreate(Bundle savedInstanceState) {
25. super.onCreate(savedInstanceState);
26. setContentView(R.layout.main);
27. //調用創建視圖的函數
28. creadView();
29. //調用性別選擇的函數
30. sexChoose();
31. //調用Button註冊監聽器的函數
32. setListener();
33. }
34.
35. //響應Button事件的函數
36. private void setListener() {
37. countButton.setOnClickListener(countListner);
38. }
39.
40. private OnClickListener countListner = new OnClickListener() {
41.
42. @Override
43. public void onClick(View v) {
44. // TODO Auto-generated method stub
45. Toast.makeText(BMIActivity.this, "你是一位"+sexChoose()+"\n"
46. +"你的身高為"+Double.parseDouble(heighText.getText().toString())+"cm"
47. +"\n你的標準體重為"+getWeight(sexChoose(), height)+"kg", Toast.LENGTH_LONG)
48. .show();
49. }
50. };
51.
52. //性別選擇的函數
53. private String sexChoose(){
54. if (maleBtn.isChecked()) {
55. sex = "男性";
56. }
57. else if(femaleBtn.isChecked()){
58. sex = "女性";
59. }
60. return sex;
61. }
62.
63. //創建視圖的函數
64. public void creadView(){
65. //txt=(TextView)findViewById(R.id.txt);
66. countButton=(Button)findViewById(R.id.btn);
67. heighText=(EditText)findViewById(R.id.etx);
68. maleBtn=(RadioButton)findViewById(R.id.male);
69. femaleBtn=(RadioButton)findViewById(R.id.female);
70. //txt.setBackgroundResource(R.drawable.bg);
71. }
72.
73. //標準體重格式化輸出的函數
74. private String format(double num) {
75. NumberFormat formatter = new DecimalFormat("0.00");
76. String str = formatter.format(num);
77. return str;
78. }
79.
80. //得到標準體重的函數
81. private String getWeight(String sex, double height) {
82. height = Double.parseDouble(heighText.getText().toString());
83. String weight = "";
84. if (sex.equals("男性")) {
85. weight =format((height – 80) * 0.7);
86. }
87. else {
88. weight = format((height – 70) * 0.6);
89. }
90. return weight;
91. }
92. }
main.xml
1. <?xml version="1.0" encoding="utf-8"?>
2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3. android:orientation="vertical"
4. android:layout_width="fill_parent"
5. android:layout_height="fill_parent"
6. android:background="@drawable/pic"
7. >
8. <TextView
9. android:id="@+id/txt"
10. android:layout_width="fill_parent"
11. android:layout_height="wrap_content"
12. android:gravity="center"
13. android:text="@string/hello"
14. android:textSize="16px"
15. />
16. <TextView
17. android:layout_width="fill_parent"
18. android:layout_height="wrap_content"
19. android:text="@string/sex"
20. />
21. <RadioGroup
22. android:layout_width="fill_parent"
23. android:layout_height="wrap_content"
24. android:orientation="horizontal"
25. >
26. <RadioButton
27. android:id="@+id/male"
28. android:layout_width="wrap_content"
29. android:layout_height="wrap_content"
30. android:text="男"
31. />
32. <RadioButton
33. android:id="@+id/female"
34. android:layout_width="wrap_content"
35. android:layout_height="wrap_content"
36. android:text="女"
37. />
38. </RadioGroup>
39. <TextView
40. android:layout_width="fill_parent"
41. android:layout_height="26px"
42. android:text="@string/heigh"
43. />
44. <EditText
45. android:id="@+id/etx"
46. android:layout_width="fill_parent"
47. android:layout_height="wrap_content"
48. />
49. <Button
50. android:id="@+id/btn"
51. android:layout_width="fill_parent"
52. android:layout_height="wrap_content"
53. android:text="@string/count"
54. />
55. </LinearLayout>
效果圖:
本文出自 “IT的點點滴滴” 博客