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
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
10 android:layout_width="fill_parent"
11 android:layout_height="wrap_content"
12 android:text="@string/height" />
13
14 android:id="@+id/height"
15 android:layout_width="fill_parent"
16 android:layout_height="wrap_content"
17 android:inputType="numberDecimal"
18 />
19
20 android:layout_width="wrap_content"
21 android:layout_height="wrap_content"
22 android:text="@string/weight"
23 />
24
25 android:id="@+id/weight"
26 android:layout_width="fill_parent"
27 android:layout_height="wrap_content"
28 android:inputType="numberDecimal"
29 />
30
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
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
43 android:id="@+id/suggest"
44 android:layout_width="fill_parent"
45 android:layout_height="wrap_content"
46 android:text=""
47 />
48
49
strings.xml 默认的
1 BMI
2 Hello world!
3 Settings
4 身高
5 体重
6 计算BMI值
7 你的BMI值是
advince.xml 算出后bmi值给的建议,这里新建一个
1 你该多吃点
2 体型很不错喔
3 你该节食了
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里写了,这样看好乱,为什么呢......?