检查Edittext是否为空,仅接受数字 [英] Check if Edittext is empty that only accepts numbers

查看:59
本文介绍了检查Edittext是否为空,仅接受数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个Edittext,仅接受数字.

I have 2 Edittext which only accepts numbers.

我想确保在按下提交按钮以防止应用程序崩溃之前,这些字段不为空.

I want to make sure that the fields are not empty before the submit button is pressed to stop the application from crashing.

这些是我拥有的2个edittext字段.

These are the 2 edittext fields i have.

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";
            }

        }

    }

推荐答案

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

You can check that your EditTexts are not empty like below:

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

您可以在onClick(View)方法中使用此条件,该方法在单击按钮时调用,如下所示:

And you can use this condition inside the onClick(View) method, which is called when your button is clicked like below:

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

第二种方法,您可以创建同时设置为EditTexts的TextWatcher,并在onTextChanged()中检查两个EditText都不为空,如下所示:

The second way, you can create TextWatcher which set to both EditTexts and in onTextChanged() you can check that both EditTexts are not empty like below:

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,如下所示:

you can set this TextWatcher for your EditText like below:

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

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

Here is code that your Activity should look like:

    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

}

这篇关于检查Edittext是否为空,仅接受数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆