简体   繁体   English

如果 EditText 字段为空或输入的值等于 0,如何使 Toast 弹出?

[英]How to make Toast pop up if EditText field is empty or the inputted value is equal to 0?

First of all, I'm new to Android Studio.首先,我是 Android Studio 的新手。 I'm currently trying to make a BMI calculator app where the user has to enter their weight and height and select the unit of measurement used for both.我目前正在尝试制作一个 BMI 计算器应用程序,用户必须输入他们的体重和身高以及 select 用于两者的测量单位。 A Toast message (R.string.toastError) should pop up upon clicking a button if: (1-2) the EditText fields for weight and height are empty and (3) if the value of HeightInput is less than or equal to zero;在以下情况下,单击按钮时应弹出 Toast 消息 (R.string.toastError):(1-2) 重量和高度的 EditText 字段为空,以及 (3) 如果 HeightInput 的值小于或等于零; else, the calculation should proceed.否则,应继续计算。

The whole math part worked fine when I tested it, but when I left the fields blank, the app just crashes.当我测试它时,整个数学部分运行良好,但是当我将字段留空时,应用程序就会崩溃。 The Toast pops up though when HeightInput = 0, but not when the EditText field for Weight is left empty at the same time.虽然当 HeightInput = 0 时会弹出 Toast,但不会在 Weight 的 EditText 字段同时留空时弹出。 I think it's the way I wrote the 'if' statement that's giving me a problem.我认为这是我写“if”语句的方式给我带来了问题。

 // if edit text is empty
            if (editTextWeightInput.getText().toString().length() == 0 || editTextHeightInput.getText().toString().length() == 0 || HeightInput <= 0) {
                Toast.makeText(getApplicationContext(), R.string.toastError, Toast.LENGTH_SHORT).show();
            } else {
                    double finalheight = Math.pow((HeightInput * constantHeight), 2.00);
                    double BodyMassIndex = (WeightInput * constantWeight) / finalheight;
                    DecimalFormat BodyMassIndexFormat = new DecimalFormat("##.##");
                    TextView textViewResult = (TextView) findViewById(R.id.textViewBmiResult);
                    textViewResult.setText(BodyMassIndexFormat.format(BodyMassIndex));
                }

Here's the whole code for reference:这是整个代码供参考:

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    // weight units spinner
    final Spinner spinWeightUnit = (Spinner) findViewById(R.id.spinnerWeightUnit);
    spinWeightUnit.setOnItemSelectedListener(this);
    ArrayAdapter <CharSequence> WeightList = ArrayAdapter.createFromResource(this, R.array.WeightUnits, android.R.layout.simple_spinner_item);
    WeightList.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinWeightUnit.setAdapter(WeightList);
    
    // height units spinner
    final Spinner spinHeightUnit = (Spinner) findViewById(R.id.spinnerHeightUnit);
    spinHeightUnit.setOnItemSelectedListener(this);
    ArrayAdapter <CharSequence> HeightList = ArrayAdapter.createFromResource(this, R.array.HeightUnits, android.R.layout.simple_spinner_item);
    HeightList.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinHeightUnit.setAdapter(HeightList);
    
    // calculate button
    Button buttonCalculate = (Button) findViewById(R.id.buttonCalculate);
    buttonCalculate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            
            // declaration
            EditText editTextWeightInput = (EditText) findViewById(R.id.editTextWeightInput);
            EditText editTextHeightInput = (EditText) findViewById(R.id.editTextHeightInput);
            double WeightInput = Double.valueOf(editTextWeightInput.getText().toString());
            double HeightInput = Double.valueOf(editTextHeightInput.getText().toString());
            String finalWeightUnit = String.valueOf(spinWeightUnit.getSelectedItem());
            String finalHeightUnit = String.valueOf(spinHeightUnit.getSelectedItem());
            double constantWeight;
            double constantHeight;
            
            // weight conversion constant
            if (finalWeightUnit.equals("kilograms")) {
                constantWeight = 1.00;
            } else {
                constantWeight = 1 / 2.204623;
            }
            
            // height conversion constant
            switch (finalHeightUnit) {
                case "inches":
                    constantHeight = 0.0254;
                    break;
                case "centimeters":
                    constantHeight = 0.01;
                    break;
                case "feet":
                    constantHeight = 1 / 3.2808;
                    break;
                default:
                    constantHeight = 1.00;
                    break;
            }
            
            // if edit text is empty
            if (editTextWeightInput.getText().toString().length() == 0 || editTextHeightInput.getText().toString().length() == 0 || HeightInput <= 0) {
                Toast.makeText(getApplicationContext(), R.string.toastError, Toast.LENGTH_SHORT).show();
            } else {
                    double finalheight = Math.pow((HeightInput * constantHeight), 2.00);
                    double BodyMassIndex = (WeightInput * constantWeight) / finalheight;
                    DecimalFormat BodyMassIndexFormat = new DecimalFormat("##.##");
                    TextView textViewResult = (TextView) findViewById(R.id.textViewBmiResult);
                    textViewResult.setText(BodyMassIndexFormat.format(BodyMassIndex));
                }
            }
        });
    }

@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {

}



  @Override
    public void onNothingSelected(AdapterView<?> adapterView) {

    }
}

I'd appreciate any help!我会很感激任何帮助! Thanks!谢谢!

Try below in if statement在 if 语句中尝试下面

if (editTextWeightInput.getText().toString().trim().isEmpty() || editTextHeightInput.getText().toString().trim().isEmpty() || HeightInput <= 0)

You need to validate your declaration code also.您还需要验证您的声明代码。

     // declaration
 EditText editTextWeightInput = (EditText) findViewById(R.id.editTextWeightInput);
        EditText editTextHeightInput = (EditText) findViewById(R.id.editTextHeightInput);

      if(TextUtil.isEmpty(editTextWeightInput.getText().toString())||TextUtil.isEmpty(editTextHeightInput.getText().toString())){
            Toast.makeText(getApplicationContext(), R.string.toastError, 
                Toast.LENGTH_SHORT).show();
    
       }else{

            
        double WeightInput = Double.valueOf(editTextWeightInput.getText().toString());
        double HeightInput = Double.valueOf(editTextHeightInput.getText().toString());
        String finalWeightUnit = String.valueOf(spinWeightUnit.getSelectedItem());
        String finalHeightUnit = String.valueOf(spinHeightUnit.getSelectedItem());
        double constantWeight;
        double constantHeight;

} }

When you leave an EditText Empty the getString() function returns a null value.当您将 EditText 留空时,getString() function 返回 null 值。 First you need to check for null in the if condition.首先,您需要在 if 条件下检查 null。

if (editTextWeightInput.getText() == null || editTextWeightInput.getText().toString().length() == 0 || HeightInput <= 0) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM