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查看jar包源码问题
  3. Android(安卓)学习资料
  4. Android系统进程Zygote启动过程的源代码分析(2)
  5. 在android中显示网络图片及查看页面源代码
  6. Android
  7. Android(安卓)内核源代码交叉编译
  8. Android源代码下载指南(图解)
  9. Android系统进程Zygote启动过程的源代码分析

随机推荐

  1. 在android使用i2c tools访问i2c
  2. android google map的使用
  3. 细数Android Bug
  4. react中使用微信jssdk分享总结
  5. Android新线程中更新主线程UI中的View方
  6. Android buttom textview 颜色平滑过渡的
  7. vue+cordova移动端Android开发环境配置
  8. Android OpenGL ES 分析与实践
  9. 在Eclips中更改Android工程中包的名字
  10. Android 下载apk提示更新以及解决Android