BMI体质指数计算器
一、学习提示
1.1、项目目标:开发一款体质指数计算器,实现输入身高和体重即可判定体型是否正常。
1.2、知识点:Activity;布局;Widget组件(EditText/Button/TextView);属性菜单;Intent。
1.3、技能目标:能使用ADT可视化布局设计器设计基本的程序界面;在开发过程中建立重构项目代码的意识。
二、项目
2.1 、BMI界面设计
相关组件:3个TextView,两个用来输入浮点小数的EditText,一个Button
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignTop="@+id/editHeight"
android:layout_marginLeft="16dp"
android:text="身高(cm)" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignRight="@+id/textView1"
android:layout_marginTop="16dp"
android:text="体重(KG)" />
<EditText
android:id="@+id/editWeight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button"
android:layout_alignTop="@+id/textView2"
android:ems="10"
android:inputType="numberDecimal" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/editHeight"
android:layout_marginTop="30dp"
android:text="速速算出小美女的体质指数来" />
<EditText
android:id="@+id/editResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button"
android:layout_below="@+id/button"
android:layout_marginTop="82dp"
android:ems="10"
android:inputType="numberDecimal" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/editResult"
android:layout_alignLeft="@+id/editResult"
android:layout_marginBottom="49dp"
android:text="鉴定结果" />
<EditText
android:id="@+id/editHeight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editWeight"
android:layout_below="@+id/editWeight"
android:layout_marginTop="22dp"
android:ems="10"
android:inputType="numberDecimal" />
</RelativeLayout>
2.2、BMI功能实现
2.2.1原理分析:Body Mass Index即“体质指数”,计算方法:体重除以身高的平方,其公式为:体质指数(BMI)=体重(kg)/(身高(m)*身高(m))。
2.2.2实现代码
(1)成员变量定义,他们是程序界面上的控件/组件,需要在代码中用到
//身高输入框,体重输入框,计算按钮,体型显示结果
private EditText editHeight;
private EditText editWeight;
private Button bthCalc;
private TextView textResult;
(2)在OnCreate()方法中设置当前Activity显示的界面,来自前面设计好的xml格式的布局文件
setContentView(R.layout.activity_main);
(3)找到OnCreate()方法中在setContentView()方法下加入初始化控件
editHeight=(EditText) findViewById(R.id.editHeight);
editWeight=(EditText) findViewById(R.id.editWeight);
bthCalc=(Button) findViewById(R.id.button);
textResult=(TextView) findViewById(R.id.editResult);
(4)在OnCreate()方法中编写计算按钮的单击事件代码
//响应按钮事件
bthCalc.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
try{
double h=Double.parseDouble(editHeight.getText().toString())/100;
double w=Double.parseDouble(editWeight.getText().toString());
//计算BIM
double bmi=w/(h*h);
if(bmi>18.5){
textResult.setText("你的体型偏瘦,需要多吃点");
}
else if(bmi>24.9){
textResult.setText("你的体型偏胖,需要少吃点");
}
else{
textResult.setText("你的体型不错哟,继续保持");
}
}catch(Exception e){
Toast.makeText(MainActivity.this, "提示:输入有误", Toast.LENGTH_SHORT).show();
}
}
});
2.3、BMI重构
发现:在设计界面上每个控件上会显示一个黄色的感叹号小图标,或者在xml布局文件中将鼠标移至黄色波浪线也会有黄色的提示信息。
问题:不应该在界面布局文件中“硬编码字符串”,也就是不应该使用字符串常量,而应该使用字符串资源,也是为了将来程序的“国际化”考虑的。
解决:对BMI项目进行重构,重构的目的:把硬编码的文字内容转移到Android资源中去