输入时验证数字输入

时间:2017-09-23 06:47:30

标签: android

我已经为付款实施了EditText。根据我输入的数字,从初始金额中减去该金额,并通过TextWatcher在上面的TextView中显示。就像这样。

enter image description here

enter image description here

enter image description here

我正在使用TextWatcher来实现这一目标。

et_EnterInstallment.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {

            //To decrease the amount entered from the price (amount as displayed in textview)
            String editTextValue = et_EnterInstallment.getText().toString();

            if(!editTextValue.isEmpty()) {
                int mainPrice = Integer.parseInt(price);
                int enteredPrice = Integer.parseInt(et_EnterInstallment.getText().toString());

                int valueAfterDeduction = mainPrice - enteredPrice;

                tv_Price.setText(String.valueOf(valueAfterDeduction));
            } else {
                tv_Price.setText(price);
            }
        }
    });

如何验证EditText值以实现以下目的:

1)如果输入的值大于价格。在这种情况下,用户无法在字段中输入该号码(如果价格为3000,用户已输入350,而他即将输入另一个0,则价格会更高,因此他将无法在EditText中输入该值)

2)禁止用户输入0或00。

2 个答案:

答案 0 :(得分:2)

这将验证您的输入。通过检查输入,因为它已被更改。

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {

    if(new String(s.toString()).equals("")){
        // we don't want to try and parse an empty string to int or
        // check for changes when the input is deleted.

    }
    else if(new String(s.toString()).equals("0")){

        et_EnterInstallment.setText("");

        Toast.makeText(context,"Do not enter an amount starting with 0",
                Toast.LENGTH_LONG).show();


    }
    else if(Integer.parseInt(s.toString()) >3000){ // put price in there.
        et_EnterInstallment.setText(s.toString().substring(0, s.toString().length() - 1));
        et_EnterInstallment.setSelection(et_EnterInstallment.length());
        Toast.makeText(context,"You cannot enter an installment " +
                        "greater than the total",
                Toast.LENGTH_LONG).show();

    }
}

不要忘记只允许xml中的数字输入:

android:inputType="number"

我使用硬编码的价格来测试这个。

一个简单的github sample

答案 1 :(得分:-1)

    et_EnterInstallment.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            String editTextValue = et_EnterInstallment.getText().toString();
            if (!editTextValue.isEmpty()) {
                if (editTextValue.length()>0&& editTextValue.charAt(0)=='0') {
                    s.replace(0, 1, "");
                    et_EnterInstallment.setError("Please enter valid number");
                } else {
                    int mainPrice = Integer.parseInt(price);
                    int enteredPrice = Integer.parseInt(et_EnterInstallment.getText().toString());

                    if (enteredPrice > mainPrice) {
                        s.clear();
                        et_EnterInstallment.setError("Please enter valid number");
                    } else {
                        int valueAfterDeduction = mainPrice - enteredPrice;

                        tv_Price.setText(String.valueOf(valueAfterDeduction));
                    }
                }
            } 
        }
    });