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. public void showBMI(){
  43. double height,weight;
  44. DecimalFormat dm=new DecimalFormat("0.00");
  45. final EditText fieldheight=(EditText)findViewById(R.id.editheight);
  46. final EditText fieldweight=(EditText)findViewById(R.id.editweight);
  47. String heightstr=fieldheight.getText().toString();
  48. String weightstr=fieldweight.getText().toString();
  49. if("".equals(fieldheight.getText().toString())
  50. ||"".equals(fieldweight.getText().toString())
  51. ||!isNumeric(heightstr)
  52. ||!isNumeric(weightstr))
  53. {
  54. new AlertDialog.Builder(this).setTitle("警告").setMessage("对不起,您输入不正确,请重新输入!").setPositiveButton("关闭",new DialogInterface.OnClickListener() {

  55. @Override
  56. public void onClick(DialogInterface dialog, int which) {
  57. // TODO Auto-generated method stub
  58. fieldheight.setText("");
  59. fieldweight.setText("");

  60. }
  61. }).show();

  62. }
  63. else
  64. {
  65. height=Double.parseDouble(fieldheight.getText().toString())/100;
  66. weight=Double.parseDouble(fieldweight.getText().toString());
  67. if(height<0.3||height>2.7||weight<5||weight>600){
  68. new AlertDialog.Builder(this).setTitle("测试结果").setMessage("啊哦,你不是来自地球的,快回火星去吧!").setPositiveButton("关闭",new DialogInterface.OnClickListener() {

  69. @Override
  70. public void onClick(DialogInterface dialog, int which) {
  71. // TODO Auto-generated method stub
  72. fieldheight.setText("");
  73. fieldweight.setText("");

  74. }
  75. }).show();
  76. }
  77. else{
  78. double bmi=weight/(height*height);
  79. String mes;
  80. if(bmi>25) mes="你的BMI值是"+dm.format(bmi)+",拜托,你吃撑了吧,太重啦!!!";
  81. else if(bmi<20) mes="你的BMI值是"+dm.format(bmi)+",啊哦,太可怜了,你太瘦啦,该补补啦!!!";
  82. else mes="你的BMI值是"+dm.format(bmi)+",不错,这还差不多!!!";
  83. new AlertDialog.Builder(this).setTitle("测试结果").setMessage(mes).setPositiveButton("关闭",new DialogInterface.OnClickListener() {

  84. @Override
  85. public void onClick(DialogInterface dialog, int which) {
  86. // TODO Auto-generated method stub
  87. fieldheight.setText("");
  88. fieldweight.setText("");

  89. }
  90. }).show();
  91. }
  92. }
  93. }
  94. public static boolean isNumeric(String str){
  95. for(int i=str.length();--i>=0;){
  96. int chr=str.charAt(i);
  97. if(chr<48 || chr>57)
  98. return false;
  99. }
  100. return true;
  101. }
  102. }
复制代码

更多相关文章

  1. Android计算器源码
  2. Android系统进程Zygote启动过程的源代码分析(2)
  3. Android源代码下载指南(图解)
  4. Android系统进程Zygote启动过程的源代码分析
  5. Android 4.1源代码今日将发布

随机推荐

  1. Android(安卓)Weekly - 第 165 期
  2. android图书管理系统+javaweb后台服务器
  3. Android(安卓)Looper和Handler
  4. Android问题之res/raw和assets文件大小限
  5. android检测新版本并下载安装的方法
  6. 深入理解onSaveInstanceState & onRestor
  7. android事件分发机制(上)
  8. Android(安卓)WebView的loadData方法注意
  9. Android(安卓)Studio打包签名全过程
  10. android初学常见问题[持续更新]