BMI计算器应用程序

时间:2017-09-22 00:27:10

标签: javascript android calc

我正在创建一个简单的BMI计算器来帮助学习javascript。我有以下基本代码,但有几个关于如何扩展功能的问题。

  1. 有没有办法可以生成用户点击“计算BMI”时显示的图片?

  2. 有没有办法可以创建一个版本,当你点击“计算BMI”时,不会返回实际计算的BMI值,而是从描述性词列表中返回一个单词。例如,我输入身高和体重,然后按“计算BMI”,应用程序显示“精彩”或“美丽”等结果?

  3. 布局:

        <?xml version="1.0" encoding="utf-8"?>
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/activity_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:orientation="vertical"
        tools:context="com.ssaurel.bmicalculator.MainActivity">
    
        <TextView
        android:text="@string/weight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:gravity="center_horizontal"
        android:layout_marginTop="50dp"
        android:textSize="20sp"/>
    
        <EditText
        android:id="@+id/weight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="10dp"
        android:ems="6"
        android:inputType="number|numberDecimal"
        android:textSize="20sp"/>
    
        <TextView
        android:text="@string/height"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:gravity="center_horizontal"
        android:layout_marginTop="50dp"
        android:textSize="20sp"/>
    
        <EditText
        android:id="@+id/height"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="10dp"
        android:ems="6"
        android:inputType="number|numberDecimal"
        android:textSize="20sp"/>
    
        <Button
        android:id="@+id/calc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="25dp"
        android:onClick="calculateBMI"
        android:text="@string/calculateBMI"
        />
    
        <TextView
        android:id="@+id/result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:gravity="center_horizontal"
        android:layout_marginTop="25dp"
        android:textSize="20sp"/>
    
        </LinearLayout>
    

    主要活动:

        package com.ssaurel.bmicalculator;
        import android.os.Bundle;
        import android.support.v7.app.AppCompatActivity;
        import android.view.View;
        import android.widget.EditText;
        import android.widget.TextView;
    
        public class MainActivity extends AppCompatActivity {
    
        private EditText height;
        private EditText weight;
        private TextView result;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        height = (EditText) findViewById(R.id.height);
        weight = (EditText) findViewById(R.id.weight);
        result = (TextView) findViewById(R.id.result);
        }
    
        public void calculateBMI(View v) {
        String heightStr = height.getText().toString();
        String weightStr = weight.getText().toString();
    
        if (heightStr != null && !"".equals(heightStr)
        && weightStr != null && !"".equals(weightStr)) {
        float heightValue = Float.parseFloat(heightStr) / 100;
        float weightValue = Float.parseFloat(weightStr);
    
        float bmi = weightValue / (heightValue * heightValue);
    
        displayBMI(bmi);
        }
        }
    
        private void displayBMI(float bmi) {
        String bmiLabel = "";
    
        if (Float.compare(bmi, 15f) <= 0) {
        bmiLabel = getString(R.string.very_severely_underweight);
        } else if (Float.compare(bmi, 15f) > 0 && Float.compare(bmi, 16f) <= 0) {
        bmiLabel = getString(R.string.severely_underweight);
        } else if (Float.compare(bmi, 16f) > 0 && Float.compare(bmi, 18.5f) <= 0) {
        bmiLabel = getString(R.string.underweight);
        } else if (Float.compare(bmi, 18.5f) > 0 && Float.compare(bmi, 25f) <= 0) {
        bmiLabel = getString(R.string.normal);
        } else if (Float.compare(bmi, 25f) > 0 && Float.compare(bmi, 30f) <= 0) {
        bmiLabel = getString(R.string.overweight);
        } else if (Float.compare(bmi, 30f) > 0 && Float.compare(bmi, 35f) <= 0) {
        bmiLabel = getString(R.string.obese_class_i);
        } else if (Float.compare(bmi, 35f) > 0 && Float.compare(bmi, 40f) <= 0) {
        bmiLabel = getString(R.string.obese_class_ii);
        } else {
        bmiLabel = getString(R.string.obese_class_iii);
        }
    
        bmiLabel = bmi + "\n\n" + bmiLabel;
        result.setText(bmiLabel);
        }
        }
    

    非常感谢任何帮助。

1 个答案:

答案 0 :(得分:-1)

使用语句来决定显示哪条消息。

`if(BMI>value){
    Textbox.setText("message")
}