android入门级小软件身高体重计算器源代码,初学者可以看看~

来源:互联网 发布:厦门长庚网络挂号 编辑:程序博客网 时间:2024/04/28 03:40
android入门级小软件身高体重计算器源代码,初学者可以看看~
一,BMI(身高体重计算器)源代码
  1. package com.android.bmi;

  2. import java.text.DecimalFormat;

  3. import android.app.Activity;
  4. import android.app.AlertDialog;
  5. import android.content.DialogInterface;
  6. import android.os.Bundle;
  7. import android.view.View;
  8. import android.view.Window;
  9. import android.view.WindowManager;
  10. import android.view.View.OnClickListener;
  11. import android.widget.EditText;

  12. public class BMI extends Activity implements OnClickListener {
  13.     /** Called when the activity is first created. */
  14.     @Override
  15.     public void onCreate(Bundle savedInstanceState) {
  16.         super.onCreate(savedInstanceState);
  17.         super.requestWindowFeature(Window.FEATURE_NO_TITLE);
  18.         super.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
  19.         setContentView(R.layout.main);
  20.         View confirm=findViewById(R.id.confirm);
  21.         confirm.setOnClickListener(this);
  22.         View cancel=findViewById(R.id.cancel);
  23.         cancel.setOnClickListener(this);
  24.         View finish=findViewById(R.id.finish);
  25.         finish.setOnClickListener(this);
  26.     }
  27.     public void onClick(View v){
  28.             switch(v.getId())
  29.             {
  30.             case R.id.confirm:
  31.                     showBMI();
  32.                     break;
  33.             case R.id.cancel:
  34.                     EditText fieldheight=(EditText)findViewById(R.id.editheight);
  35.                 EditText fieldweight=(EditText)findViewById(R.id.editweight);
  36.                     fieldheight.setText("");
  37.                         fieldweight.setText("");
  38.                     break;
  39.             case R.id.finish:finish();
  40.             
  41.             }
  42.     }
  43.     public void showBMI(){
  44.             double height,weight;
  45.             DecimalFormat dm=new DecimalFormat("0.00");
  46.             final EditText fieldheight=(EditText)findViewById(R.id.editheight);
  47.             final EditText fieldweight=(EditText)findViewById(R.id.editweight);
  48.             String heightstr=fieldheight.getText().toString();
  49.             String weightstr=fieldweight.getText().toString();
  50.             if("".equals(fieldheight.getText().toString())
  51.                             ||"".equals(fieldweight.getText().toString())
  52.                             ||!isNumeric(heightstr)
  53.                             ||!isNumeric(weightstr))
  54.             {
  55.                     new AlertDialog.Builder(this).setTitle("警告").setMessage("对不起,您输入不正确,请重新输入!").setPositiveButton("关闭",new DialogInterface.OnClickListener() {
  56.                             
  57.                             @Override
  58.                             public void onClick(DialogInterface dialog, int which) {
  59.                                     // TODO Auto-generated method stub
  60.                                     fieldheight.setText("");
  61.                                     fieldweight.setText("");
  62.                                     
  63.                             }
  64.                     }).show();
  65.                 
  66.             }
  67.                else
  68.             {
  69.             height=Double.parseDouble(fieldheight.getText().toString())/100;
  70.             weight=Double.parseDouble(fieldweight.getText().toString());
  71.             if(height<0.3||height>2.7||weight<5||weight>600){
  72.                     new AlertDialog.Builder(this).setTitle("测试结果").setMessage("啊哦,你不是来自地球的,快回火星去吧!").setPositiveButton("关闭",new DialogInterface.OnClickListener() {
  73.                             
  74.                             @Override
  75.                             public void onClick(DialogInterface dialog, int which) {
  76.                                     // TODO Auto-generated method stub
  77.                                     fieldheight.setText("");
  78.                                     fieldweight.setText("");
  79.                                     
  80.                             }
  81.                         }).show();
  82.             }
  83.             else{
  84.                     double bmi=weight/(height*height);
  85.                     String mes;
  86.                     if(bmi>25) mes="你的BMI值是"+dm.format(bmi)+",拜托,你吃撑了吧,太重啦!!!";
  87.                     else if(bmi<20) mes="你的BMI值是"+dm.format(bmi)+",啊哦,太可怜了,你太瘦啦,该补补啦!!!";
  88.                     else mes="你的BMI值是"+dm.format(bmi)+",不错,这还差不多!!!";
  89.                     new AlertDialog.Builder(this).setTitle("测试结果").setMessage(mes).setPositiveButton("关闭",new DialogInterface.OnClickListener() {
  90.                         
  91.                         @Override
  92.                         public void onClick(DialogInterface dialog, int which) {
  93.                                 // TODO Auto-generated method stub
  94.                                 fieldheight.setText("");
  95.                                 fieldweight.setText("");
  96.                                 
  97.                         }
  98.                     }).show();
  99.                     }
  100.             }
  101.     }
  102.     public static boolean isNumeric(String str){      
  103.                for(int i=str.length();--i>=0;){      
  104.                   int chr=str.charAt(i);      
  105.                   if(chr<48 || chr>57)      
  106.                      return false;      
  107.                }      
  108.                return true;      
  109.             }   
  110. }
复制代码