检查Edittext是否为空,只接受数字

时间:2015-01-10 13:22:58

标签: android android-edittext

我有2个只接受数字的Edittext。

我想确保在按下提交按钮以停止应用程序崩溃之前字段不为空。

这是我拥有的2个edittext字段。

EditText weightText = (EditText)findViewById(R.id.WeightText);
EditText heightText = (EditText)findViewById(R.id.Heighttext);

代码:

public class BMI extends Activity {

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.bmi);
        }


        public void calculateHandler(View view) {
            // make sure we handle the click of the calculator button

            if (view.getId() == R.id.calculate) {

                // get the references to the widgets
                EditText weightText = (EditText)findViewById(R.id.WeightText);
                EditText heightText = (EditText)findViewById(R.id.Heighttext);
                TextView resultText = (TextView)findViewById(R.id.resultLabel);



                // get the users values from the widget references

                float weight = Float.parseFloat(weightText.getText().toString());
                float height = Float.parseFloat(heightText.getText().toString());

                // calculate the bmi value

                float bmiValue = calculateBMI(weight, height);

                // interpret the meaning of the bmi value
                String bmiInterpretation = interpretBMI(bmiValue);

                // now set the value in the result text

                resultText.setText(bmiValue + " = " + bmiInterpretation);
            }
        }

        private float calculateBMI (float weight, float height) {


            return (float)Math.round((weight / (height * height)) * 100) / 100 ;
        }


        // interpret what BMI means
        private String interpretBMI(float bmiValue) {

            if (bmiValue < 16) {
                return "Severely underweight";
            } else if (bmiValue < 18.5) {

                return "Underweight";
            } else if (bmiValue < 25) {

                return "Normal";
            } else if (bmiValue < 30) {

                return "Overweight";
            } else {
                return "Obese";
            }

        }

    }

3 个答案:

答案 0 :(得分:1)

您可以检查您的EditTexts是否为空,如下所示:

weightText.getText().length() != 0 && heightText.getText().length() != 0

你可以在onClick(View)方法中使用这个条件,当点击你的按钮时会调用它,如下所示:

 yourButton.setOnClickListener( new OnClickListener(){
   public void onClick(){
       if(weightText.getText().length() != 0 && heightText.getText().length() != 0){
          //do sth
       }
 });

第二种方法,你可以创建设置为EditTexts和onTextChanged()的TextWatcher,你可以检查两个EditTexts都不是空的,如下所示:

private class YourTextWatcher implements TextWatcher{
    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
        yourButton.setEnabled(weightText.getText().length() != 0 && heightText.getText().length() != 0)
    }

    @Override
    public void afterTextChanged(Editable editable) {

    }
}

您可以为EditText设置此TextWatcher,如下所示:

     weightText.addTextChangedListener(new YourTextWatcher());
     heightText.addTextChangedListener(new YourTextWatcher());

以下是您的活动应如下的代码:

    public class BMI extends Activity {

        EditText weightText;
        EditText heightText;
        TextView resultText;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.bmi);
            weightText = (EditText) findViewById(R.id.WeightText);
            heightText = (EditText) findViewById(R.id.Heighttext);
            resultText = (TextView) findViewById(R.id.resultLabel);


            Button button = (Button) findViewById(R.id.calulate);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    calculateHandler();
                }
            });
        }


        public void calculateHandler() {
            // make sure we handle the click of the calculator button
            if (weightText.getText().length() == 0 || heightText.getText().length() == 0) {
                return;
            }
            // get the users values from the widget references

            float weight = Float.parseFloat(weightText.getText().toString());
            float height = Float.parseFloat(heightText.getText().toString());

            // calculate the bmi value

            float bmiValue = calculateBMI(weight, height);

            // interpret the meaning of the bmi value
            String bmiInterpretation = interpretBMI(bmiValue);

            // now set the value in the result text
            resultText.setText(bmiValue + " = " + bmiInterpretation);
            // display toast additionally to example
            Toast.makeText(this, bmiValue + " = " + bmiInterpretation, Toast.LENGTH_LONG).show();
        }

        private float calculateBMI(float weight, float height) {
            return (float) Math.round((weight / (height * height)) * 100) / 100;
        }


        // interpret what BMI means
        private String interpretBMI(float bmiValue) {
            if (bmiValue < 16) {
                return "Severely underweight";
            } else if (bmiValue < 18.5) {

                return "Underweight";
            } else if (bmiValue < 25) {

                return "Normal";
            } else if (bmiValue < 30) {

                return "Overweight";
            } else {
                return "Obese";
            }
        }
    }

计算方法

public void calculateHandler() {
    // make sure we handle the click of the calculator button
    if (weightText.getText().toString().trim().length() == 0 || heightText.getText().toString().trim().length() == 0) {
        Toast.makeText(this, "Please fill all field by numbers", Toast.LENGTH_LONG).show();
        return;
    }
    // get the users values from the widget references

    float weight = Float.parseFloat(weightText.getText().toString());
    float height = Float.parseFloat(heightText.getText().toString());

    // calculate the bmi value

    float bmiValue = calculateBMI(weight, height);

    // interpret the meaning of the bmi value
    String bmiInterpretation = interpretBMI(bmiValue);

    // now set the value in the result text
    resultText.setText(bmiValue + " = " + bmiInterpretation);
    // display toast additionally to example

}

答案 1 :(得分:1)

weightText.getText().toString().isEmpty();

答案 2 :(得分:0)

试试这个..

String weight = weightText.getText().toString();
String height = heightText.getText().toString();

if((Integer.parseInt(weight)!=null) && (Integer.parseInt(height)!=null)){ 
    //Not an empty
 }
  else{
  //empty
}