- package com.android.bmi;
-
- import java.text.DecimalFormat;
-
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.content.DialogInterface;
- import android.os.Bundle;
- import android.view.View;
- import android.view.Window;
- import android.view.WindowManager;
- import android.view.View.OnClickListener;
- import android.widget.EditText;
-
- public class BMI extends Activity implements OnClickListener {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- super.requestWindowFeature(Window.FEATURE_NO_TITLE);
- super.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
- setContentView(R.layout.main);
- View confirm=findViewById(R.id.confirm);
- confirm.setOnClickListener(this);
- View cancel=findViewById(R.id.cancel);
- cancel.setOnClickListener(this);
- View finish=findViewById(R.id.finish);
- finish.setOnClickListener(this);
- }
- public void onClick(View v){
- switch(v.getId())
- {
- case R.id.confirm:
- showBMI();
- break;
- case R.id.cancel:
- EditText fieldheight=(EditText)findViewById(R.id.editheight);
- EditText fieldweight=(EditText)findViewById(R.id.editweight);
- fieldheight.setText("");
- fieldweight.setText("");
- break;
- case R.id.finish:finish();
-
- }
- }
- public void showBMI(){
- double height,weight;
- DecimalFormat dm=new DecimalFormat("0.00");
- final EditText fieldheight=(EditText)findViewById(R.id.editheight);
- final EditText fieldweight=(EditText)findViewById(R.id.editweight);
- String heightstr=fieldheight.getText().toString();
- String weightstr=fieldweight.getText().toString();
- if("".equals(fieldheight.getText().toString())
- ||"".equals(fieldweight.getText().toString())
- ||!isNumeric(heightstr)
- ||!isNumeric(weightstr))
- {
- new AlertDialog.Builder(this).setTitle("警告").setMessage("对不起,您输入不正确,请重新输入!").setPositiveButton("关闭",new DialogInterface.OnClickListener() {
-
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- fieldheight.setText("");
- fieldweight.setText("");
-
- }
- }).show();
-
- }
- else
- {
- height=Double.parseDouble(fieldheight.getText().toString())/100;
- weight=Double.parseDouble(fieldweight.getText().toString());
- if(height<0.3||height>2.7||weight<5||weight>600){
- new AlertDialog.Builder(this).setTitle("测试结果").setMessage("啊哦,你不是来自地球的,快回火星去吧!").setPositiveButton("关闭",new DialogInterface.OnClickListener() {
-
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- fieldheight.setText("");
- fieldweight.setText("");
-
- }
- }).show();
- }
- else{
- double bmi=weight/(height*height);
- String mes;
- if(bmi>25) mes="你的BMI值是"+dm.format(bmi)+",拜托,你吃撑了吧,太重啦!!!";
- else if(bmi<20) mes="你的BMI值是"+dm.format(bmi)+",啊哦,太可怜了,你太瘦啦,该补补啦!!!";
- else mes="你的BMI值是"+dm.format(bmi)+",不错,这还差不多!!!";
- new AlertDialog.Builder(this).setTitle("测试结果").setMessage(mes).setPositiveButton("关闭",new DialogInterface.OnClickListener() {
-
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- fieldheight.setText("");
- fieldweight.setText("");
-
- }
- }).show();
- }
- }
- }
- public static boolean isNumeric(String str){
- for(int i=str.length();--i>=0;){
- int chr=str.charAt(i);
- if(chr<48 || chr>57)
- return false;
- }
- return true;
- }
- }
标准体重的测量
最新推荐文章于 2022-09-08 11:25:13 发布