1

我对 Android 应用程序开发还很陌生,只是学习制作一些简单的应用程序。所以我决定做一个BMI计算器。我制作了一个界面,用户可以使用微调器为他们的身高和体重选择单位。然后,单击按钮后,我会将 EditText 上的值的字符串解析为 double。还获得了用户选择的微调器项目(高度和体重的单位)。我设法通过使用 setText 获得正确的值并返回用户输入的值和单位。但是,这些值并没有按照我想要的方式转换。if-else 语句在这里可能有问题。

    package com.dirk.myfirstapp;

    import android.app.Activity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Spinner;
    import android.widget.TextView;

    public class MainActivity extends Activity {

        TextView output;
        EditText weightInput, heightInput;
        Button calculate;
        double height, weight, BMI;
        String heightInputString, weightInputString, weightUnit, heightUnit;
        Spinner heightUnitSpinner, weightUnitSpinner;


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            Button calculate = (Button) findViewById(R.id.button1);
            output = (TextView) findViewById(R.id.textView4);
            weightInput = (EditText) findViewById(R.id.editText1);
            heightInput = (EditText) findViewById(R.id.editText2);
            heightUnitSpinner = (Spinner) findViewById(R.id.spinnerHeight);
            weightUnitSpinner = (Spinner) findViewById(R.id.spinnerWeight);

            calculate.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    //PARSE STRINGS
                    heightInputString = heightInput.getText().toString();
                    height = Double.parseDouble(heightInputString);
                    weightInputString = weightInput.getText().toString();
                    weight = Double.parseDouble(weightInputString);
                    weightUnit = weightUnitSpinner.getSelectedItem().toString();
                    heightUnit = heightUnitSpinner.getSelectedItem().toString();

                    // CONVERT FIRST TO SI UNITS

                    // CONVERT WEIGHT TO kilogrammes
                    if (weightUnit == "lbs") {
                        weight = weight * 0.453592;
                    } 
                    // CONVERT HEIGHT TO meters
                    if (heightUnit == "cm") {
                height = height / 100;
                    } else if (heightUnit == "in") {
                        height = height * 0.0254;
                    }

                    BMI = weight / (height * height);
                    output.setText("Your BMI is " + BMI);


                }
            });

        }



    }
4

1 回答 1

1

当您比较字符串时,请使用 Object.equals("string"); 比较。不要使用'==''

前任:

// CONVERT HEIGHT TO meters
                    if (heightUnit.equals("cm")) {
                height = height / 100;
                    } else if (heightUnit.equals("in")) {
                        height = height * 0.0254;
                    }
于 2013-05-10T18:52:53.383 回答