目的
此次实验设计和实现一个BMI计算程序,并发布真机或者模拟器运行。(本实验可以参照教材Android移动开发教程项目式第二章内容,并进行改造)。
要求
a) 开发输入界面。还需要增加身高
b) 进行事件处理。
c) 显示计算结果。
d) 发布到手机。‘
输入项为:
身高, 体重, 选择性别
输出如下:
某某某先生/女生,您已经超重,请加强锻炼,少吃肉。
代码
package com.example.myapplication;
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.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import java.math.BigDecimal;
public class MainActivity extends Activity
{
//计算
private Button calculatebtn;
private EditText hightET,weightET;
// private RadioGroup sex;
private RadioButton frbtn,mrbtn;
private TextView restv;
private String sextxt ;
private String []bmiclassify={"身体健康,请继续保持!","您已经超重,请加强锻炼,少吃肉。","您已经轻度肥胖,请加强锻炼,注意饮食。",
"您已经重度肥胖,请加强锻炼,注意身体健康。","您已经重度肥胖,注意身体健康!"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//页面布局
setContentView(R.layout.bmi);
//获得UI控件
calculatebtn =(Button) findViewById(R.id.calculate);
hightET = (EditText) findViewById(R.id.hight);
weightET = (EditText) findViewById(R.id.wight);
frbtn =(RadioButton) findViewById(R.id.female);
mrbtn = (RadioButton) findViewById(R.id.male);
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!运行时点击开始直接闪退的原因:没有连接result!!!!!
restv = (TextView) findViewById(R.id.result);
}
protected void onStart()
{
super.onStart();
//注册
registerEvent();
}
//注册事件
private void registerEvent()
{
//注册按钮事件
calculatebtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//判断是否写内容
if(!weightET.getText().toString().trim().equals("")&&!hightET.getText().toString().trim().equals(""))
{
//判断性别是否选择
if(frbtn.isChecked()||mrbtn.isChecked())
{
//判断性别
if(mrbtn.isChecked())
{
sextxt = "男士";
}
if(frbtn.isChecked())
{
sextxt = "女士";
}
//获得数据
Double weight = Double.parseDouble(weightET.getText().toString());
Double height = Double.parseDouble(hightET.getText().toString());
//计算
double bmi = weight/(height*height);
//保留两位数
BigDecimal bg = new BigDecimal(bmi);
double bmi1 = bg.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
//组成输出内容
StringBuffer sb = new StringBuffer();
sb.append("<!----评估结果----->\n");
sb.append(sextxt+",您的BMI为:"+bmi1+"。\n"+bmimsg(bmi1));
//输出界面显示结果
restv.setText(sb.toString());
}else
{
showMessage("请选择性别!");
}
}else
{
showMessage("请输入身高体重!");
}
}
});
}
//判断bmi写sb
private String bmimsg(double bmi)
{
int i=0;
if(bmi>18&&bmi<=25)
i=0;
else if(bmi>25&&bmi<=30)
{
i=1;
}
else if(bmi>30&&bmi<=35)
{
i=2;
}
else if(bmi>35&&bmi<=40)
{
i=3;
}
else if(bmi>40)
{
i=4;
}
return bmiclassify[i];
}
//消息提醒
private void showMessage (String msg)
{
//提示框
AlertDialog alert = new AlertDialog.Builder(this).create();
alert.setTitle("系统信息");
alert.setMessage(msg);
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 menuItem)
{
switch (menuItem.getItemId())
{
case 1:
finish();
break;//退出
}
return super.onOptionsItemSelected(menuItem);
}
*/
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="#000000"
android:orientation="vertical"
android:layout_marginTop="20dip"
>
<!--标题-->
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="身高体重指数计算器"
android:textColor="#ffffff"
android:textSize="35dip" />
<!--身高框-->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:orientation="horizontal"
>
<TextView
android:id="@+id/textView10"
android:layout_width="190dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:text="请输入你的身高:"
android:textColor="#ffffff"
android:textSize="20dp" />
<!--用户输入-->
<EditText
android:id="@+id/hight"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:background="#ffffff"
android:textColor="#000000"
tools:ignore="TouchTargetSizeCheck" />
<TextView
android:id="@+id/textView2"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="m"
android:textColor="#ffffff"
android:textSize="18dp" />
<!--体重框-->
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:orientation="horizontal"
>
<TextView
android:id="@+id/textView1"
android:layout_width="190dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:text="请输入你的体重:"
android:textColor="#ffffff"
android:textSize="20dp" />
<EditText
android:id="@+id/wight"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:textColor="#000000"
android:background="#ffffff"/>
<TextView
android:id="@+id/textView3"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="kg"
android:textColor="#ffffff"
android:textSize="18dp" />
</LinearLayout>
<!--性别-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="horizontal"
android:layout_marginTop="25dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="@+id/textView7"
android:layout_width="215dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="请选择您的性别:"
android:textColor="#ffffff"
android:textSize="20dp" />
<!--单选组件-->
<RadioGroup
android:layout_width="300dp"
android:layout_height="100dp"
android:layout_weight="1" >
<RadioButton
android:id="@+id/female"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="女"
android:textColor="#ffffff"
android:textSize="20dp"
android:layout_marginLeft="50dp"/>
<RadioButton
android:id="@+id/male"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="男"
android:textColor="#ffffff"
android:textSize="20dp"
android:layout_marginLeft="50dp"/>
</RadioGroup>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<!--按钮-->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="80dp"
android:orientation="horizontal"
android:gravity="center_horizontal"
>
<Button
android:id="@+id/calculate"
android:layout_marginTop="20dp"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="开始评估"
android:textColor="#ffffff"
android:background="#ff00"
android:textSize="20dp"/>
</LinearLayout>
<!--结果-->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="center_horizontal"
android:layout_marginTop="20dp">
<TextView
android:id="@+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#ffffff"
android:layout_marginTop="10dp"
/>
</LinearLayout>
</LinearLayout>
运行结果