标准个人身高计算器由一个main.xml文件和HeightCalculatorActivity类实现,其完整代码如下。
(1)标准个人身高程序界面布局XML:【main.xml】。
(2)标准个人身高程序实现类:【HeightCalculatorActivity.java】。
package com.demo.pr2;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
public class HeightCalculatorActivity extends Activity {
//计算按钮
private Button calculatorButton;
//体重输入框
private EditText weightEditText;
//男性选择框
private CheckBox manCheckBox;
//女性选择框
private CheckBox womanCheckBox;
//显示结果
private TextView resultTextView;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//设置页面布局
setContentView(R.layout.main);
//从 main.xml 页面布局中获得对应的UI控件
calculatorButton=(Button)findViewById(R.id.calculator);
weightEditText=(EditText)findViewById(R.id.weight);
manCheckBox=(CheckBox)findViewById(R.id.man);
womanCheckBox=(CheckBox)findViewById(R.id.woman);
resultTextView=(TextView)findViewById(R.id.result);
}
@Override
protected void onStart() {
super.onStart();
//注册事件
registerEvent();
}
private void registerEvent()
{
//注册按钮事件
calculatorButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v){
//判断是否已填写体重数据
if(!weightEditText.getText().toString().trim().equals(""))
{
//判断是否已选择性别
if(manCheckBox.isChecked()||womanCheckBox.isChecked())
{
Double weight=Double.parseDouble(weightEditText
.getText().toString());
StringBuffer sb=new StringBuffer();
sb.append("------评估结果----- n");
if(manCheckBox.isChecked())
{
sb.append("男性标准身高:");
//执行运算
double result=evaluateHeight(weight,"男");
sb.append((int)result+"(厘米)");
}
if(womanCheckBox.isChecked())
{
sb.append("女性标准身高:");
//执行运算
double result=evaluateHeight(weight,"女");
sb.append((int)result+"(厘米)");
}
//输出页面显示结果
resultTextView.setText(sb.toString());
}else
{
showMessage("请选择性别!");
}
}else
{
showMessage("请输入体重!");
}
}
});
}
private double evaluateHeight(double weight,String sex)
{
double height;
if(sex=="男"){
height=170-(62-weight)/0.6;
}else{
height =158-(52-weight)/0.5;
}
return height;
}
private void showMessage(String message)
{
//提示框
AlertDialog alert=new AlertDialog.Builder(this).create();
alert.setTitle("系统信息");
alert.setMessage(message);
alert.setButton("确定",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int whichButton){
//可在此方法内编写按下确定按钮的处理代码,但在本项目中不需要编写处理代码
}
});
alert.show();// 显示窗口
}
**
* 创建菜单
*/
public boolean onCreateOptionsMenu(Menu menu){
menu.add(Menu.NONE,1,Menu.NONE,"退出");
return super.onCreateOptionsMenu(menu);
}
public boolean onOptionsItemSelected(MenuItem item){
// TODO Auto-generated method stub
switch(item.getItemId()){
case 1://退出
finish();
break;
}
return super.onOptionsItemSelected(item);
}
}



