繁体   English   中英

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

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

首先,我是 Android Studio 的新手。 我目前正在尝试制作一个 BMI 计算器应用程序,用户必须输入他们的体重和身高以及 select 用于两者的测量单位。 在以下情况下,单击按钮时应弹出 Toast 消息 (R.string.toastError):(1-2) 重量和高度的 EditText 字段为空,以及 (3) 如果 HeightInput 的值小于或等于零; 否则,应继续计算。

当我测试它时,整个数学部分运行良好,但是当我将字段留空时,应用程序就会崩溃。 虽然当 HeightInput = 0 时会弹出 Toast,但不会在 Weight 的 EditText 字段同时留空时弹出。 我认为这是我写“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));
                }

这是整个代码供参考:

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) {

    }
}

我会很感激任何帮助! 谢谢!

在 if 语句中尝试下面

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

您还需要验证您的声明代码。

     // 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;

}

当您将 EditText 留空时,getString() function 返回 null 值。 首先,您需要在 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