簡體   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