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里写了,这样看好乱,为什么呢......?