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

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. }
复制代码
### 回答1: 以下是一个简单的 Android BMI体质指数计算器代码: MainActivity.java: ``` import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { EditText height, weight; Button calculate; TextView result; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); height = findViewById(R.id.height); weight = findViewById(R.id.weight); calculate = findViewById(R.id.calculate); result = findViewById(R.id.result); calculate.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { double h = Double.parseDouble(height.getText().toString()); double w = Double.parseDouble(weight.getText().toString()); double bmi = w / (h * h); String message = ""; if (bmi < 18.5) { message = "您的BMI指数为" + bmi + ",属于偏瘦"; } else if (bmi >= 18.5 && bmi < 24) { message = "您的BMI指数为" + bmi + ",属于正常"; } else if (bmi >= 24 && bmi < 28) { message = "您的BMI指数为" + bmi + ",属于超重"; } else { message = "您的BMI指数为" + bmi + ",属于肥胖"; } result.setText(message); } }); } } ``` activity_main.xml: ``` <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="16dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="身高(米):" /> <EditText android:id="@+id/height" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="numberDecimal" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="体重(公斤):" /> <EditText android:id="@+id/weight" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="numberDecimal" /> <Button android:id="@+id/calculate" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="计算BMI" /> <TextView android:id="@+id/result" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="16dp" /> </LinearLayout> ``` 这个代码使用了一个简单的 UI,包括身高和体重的文本框,一个“计算BMI”按钮以及用于显示结果的文本框。当用户点击“计算BMI”按钮时,程序将计算BMI并显示结果。 ### 回答2: 以下是使用Java语言编写的Android BMI体质指数计算器代码: ```java import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends AppCompatActivity { private EditText editTextWeight; private EditText editTextHeight; private Button buttonCalculate; private TextView textViewResult; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editTextWeight = findViewById(R.id.editTextWeight); editTextHeight = findViewById(R.id.editTextHeight); buttonCalculate = findViewById(R.id.buttonCalculate); textViewResult = findViewById(R.id.textViewResult); buttonCalculate.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { calculateBMI(); } }); } private void calculateBMI() { double weight = Double.parseDouble(editTextWeight.getText().toString()); double height = Double.parseDouble(editTextHeight.getText().toString()); double bmi = weight / (height * height); String result; if (bmi < 18.5) { result = "过轻"; } else if (bmi >= 18.5 && bmi < 25) { result = "正常"; } else if (bmi >= 25 && bmi < 30) { result = "过重"; } else { result = "肥胖"; } textViewResult.setText("BMI指数:" + bmi + "\n体质指数:" + result); } } ``` 这段代码是一个简单的BMI计算器应用程序,用户可以输入体重和身高,计算并显示BMI指数体质指数BMI的计算方法是体重(千克)除以身高(米)的平方。根据BMI指数的范围,可以确定体质指数的等级,例如过轻、正常、过重和肥胖。当用户点击"计算"按钮时,会调用`calculateBMI()`方法进行计算,并将结果显示在`textViewResult`文本视图中。 (此回答仅提供了基础的计算器实现,并未包含更多的功能和错误处理。如果需要实现更完整的BMI计算器,请根据实际需求进行修改和完善。) ### 回答3: 下面是一个简单的Android BMI体质指数计算器的示例代码: MainActivity.java: ```java import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends AppCompatActivity { EditText heightInput, weightInput; Button calculateButton; TextView resultText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); heightInput = findViewById(R.id.height_input); weightInput = findViewById(R.id.weight_input); calculateButton = findViewById(R.id.calculate_button); resultText = findViewById(R.id.result_text); calculateButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 获取身高和体重输入值 float height = Float.parseFloat(heightInput.getText().toString()); float weight = Float.parseFloat(weightInput.getText().toString()); // 计算BMI值 float bmi = weight / (height * height); // 显示结果 resultText.setText("您的BMI指数为:" + bmi); } }); } } ``` activity_main.xml: ```xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <EditText android:id="@+id/height_input" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="身高(米)" android:inputType="numberDecimal"/> <EditText android:id="@+id/weight_input" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/height_input" android:hint="体重(千克)" android:inputType="numberDecimal"/> <Button android:id="@+id/calculate_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/weight_input" android:text="计算BMI"/> <TextView android:id="@+id/result_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/calculate_button" android:padding="16dp"/> </RelativeLayout> ``` 这个示例代码创建了一个包含输入身高和体重的EditText,一个计算按钮和一个用于显示结果的TextView。当用户点击计算按钮时,会从输入框中获取身高和体重的值,并计算BMI指数。最后,结果将显示在TextView中。请确保在Android Studio中创建一个新的项目,并将这些代码粘贴到相应的文件中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值