当没有输入数字时,BMI应用程序会继续崩溃

时间:2017-11-21 08:20:59

标签: java android validation

我正在写一个BMI计算器作为练习,设法做到这一切,除了一个问题。当没有输入数字并按下按钮时,应用程序会继续崩溃。我已经在我的代码中看到了setError,但它无效。有什么想法吗?

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.text.DecimalFormat;

public class MainActivity extends AppCompatActivity {

    TextView textResult;
    TextView feedbackOut;

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

        final EditText massInputText = findViewById(R.id.massText);
        final EditText heightInputText = findViewById(R.id.heightText);
        textResult = findViewById(R.id.textResult);
        final DecimalFormat df = new DecimalFormat("0.00");
        final Button buttonCalculator = findViewById(R.id.button);
        feedbackOut = findViewById(R.id.feedBack);


        buttonCalculator.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (massInputText.getText().length() == 0) {
                    massInputText.setError("Please");
                }

                if (heightInputText.getText().length() == 0) {
                    heightInputText.setError("Please");
                }

                double mass = Double.parseDouble(massInputText.getText().toString());
                double height = Double.parseDouble(heightInputText.getText().toString());
                double bmi = getMassData(mass, height);
                textResult.setText(df.format(bmi).toString());

            }
        });

    }

    public double getMassData(double mass, double height) {
        double square = Math.pow(height, 2);
        double result = mass / square;

        if (result < 18.5) {
            feedbackOut.setText("You are underweight. You need to eat more! ");
        } else if (result >= 18.5 & result <= 24.9) {
            feedbackOut.setText("You are in the Healthy weight range. Good Job! ");
        }
        if (result >= 25 & result <= 50) {
            feedbackOut.setText("You are Overweight. Start working out and eat more healthy! ");
        }

        return result;
    }

}

1 个答案:

答案 0 :(得分:0)

我建议您改进对OnClicklistener小部件附加的buttonCalculator的验证。

特别是您应该在输入无效后快速失败,提供有意义的错误消息,并指示最终用户快速了解错误发生后该怎么做。

这些方面的东西:

buttonCalculator.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        Editable massInputStr = massInputText.getText();
        if (isEmpty(massInputStr) || isNotANumber(massInputStr)) {
            massInputText.setError("Please enter the mass value (example: 12.5)");
            return; // Prevent further execution
        }

        Editable heightStr = heightInputText.getText();
        if (isEmpty(heightStr)  || isNotANumber(heightStr)) {
            heightInputText.setError("Please enter a valid height (example: 1.75)");
            return; // Prevent further execution
        }

        double mass = Double.parseDouble(massInputStr.toString());
        double height = Double.parseDouble(heightStr.toString());
        double bmi = getMassData(mass, height);
        textResult.setText(df.format(bmi));
    }

    private boolean isEmpty(Editable editable) {
        return editable == null || editable.toString().trim().isEmpty();
    }

    private boolean isNotANumber(Editable editable) {
        return !editable.toString().trim().matches("\\d+(\\.\\d+)?");
    }
});

在这种特定情况下,您应该检查输入是否为空(或者它是否只有空格)以及是否具有特定格式。您可以使用正则表达式(请参阅isNotANumber)。