Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 正在输入时验证数字输入_Android - Fatal编程技术网

Android 正在输入时验证数字输入

Android 正在输入时验证数字输入,android,Android,我已经为付款实现了EditText。根据我输入的数字,该金额从初始金额中减去,并通过TextWatcher显示在上面的TextView中,如下所示 我正在使用TextWatcher来实现这一点 et_EnterInstallment.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int

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

我正在使用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。

这将在您输入时验证您的输入。当输入发生变化时,通过检查输入

@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"
我使用price的硬编码值来测试这个


一个简单的答案。

按照这个答案。它对我有用。非常感谢你!。这是非常有效的。我添加了一行
et_entertainment.setSelection(et_entertainment.length())
将光标设置在末尾。