Java 如何禁止在edittext中输入零后的数字?

Java 如何禁止在edittext中输入零后的数字?,java,android,android-layout,android-edittext,Java,Android,Android Layout,Android Edittext,如何在edittext中禁用零后输入的数字 我只对此进行了验证 if (TextUtils.isEmpty(strBalance)) { Toast.makeText(TransactionSellINActivity.this, "Amount should not be empty", Toast.LENGTH_SHORT) .show(); } else if

如何在edittext中禁用零后输入的数字

我只对此进行了验证

 if (TextUtils.isEmpty(strBalance)) {
                    Toast.makeText(TransactionSellINActivity.this, "Amount should not be empty", Toast.LENGTH_SHORT)
                            .show();
                } else if (strBalance.equalsIgnoreCase("0")) {
                    Toast.makeText(TransactionSellINActivity.this, "Amount should not be Zero", Toast.LENGTH_SHORT)
                            .show();
                }
但是,如果用户输入01或001或02,03等。,?零后的ie数字?->我想限制那些数字


如何处理这种情况?

您也可以这样做,让用户输入任意数量的零。下面的正则表达式将从字符串中删除前导零,然后您可以继续处理字符串

private EditText  mEt;//your edittext
private TextWatcher mMoneyWatcher = 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) {

        if (!s.toString.isEmpty()) {
            mPayEtValue.removeTextChangedListener(mMoneyWatcher);
            if (s.toString.charAt(0)=='0') {
              mEt.setText(s.toString.substring(1,s.length()));  
            }

            mEt.addTextChangedListener(mMoneyWatcher);
        }
    }

    @Override
    public void afterTextChanged(Editable s) {

    }
};
mEt.addTextChangedListener(mMoneyWatcher);
if (!TextUtils.isEmpty(strBalance) && Integer.parseInt(strBalance) > 0) 
{
        strBalance=strBalance.replaceFirst("^0+(?!$)", "")       
}

您也可以这样做,让用户输入任意数量的零。下面的正则表达式将从字符串中删除前导零,然后您可以继续处理字符串

if (!TextUtils.isEmpty(strBalance) && Integer.parseInt(strBalance) > 0) 
{
        strBalance=strBalance.replaceFirst("^0+(?!$)", "")       
}

我想你应该限制那些以0开头的数字。为此,您必须使用
.startsWith()


我想你应该限制那些以0开头的数字。为此,您必须使用
.startsWith()


用户无法使用此代码在零后添加数字,请尝试

Edittext.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence arg0, int arg1, int arg2,
                int arg3) {

        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1,
                int arg2, int arg3) {

        }

        @Override
        public void afterTextChanged(Editable arg0) {

            if (Edittext.getText().toString().startsWith("0")
                    && Edittext.getText().length() > 1) {

                Edittext.setText("0");
                Edittext.setSelection(Edittext.getText().toString().length());
            }
        }
    });

也许对你有用

用户无法使用此代码在零后添加数字,请尝试

Edittext.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence arg0, int arg1, int arg2,
                int arg3) {

        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1,
                int arg2, int arg3) {

        }

        @Override
        public void afterTextChanged(Editable arg0) {

            if (Edittext.getText().toString().startsWith("0")
                    && Edittext.getText().length() > 1) {

                Edittext.setText("0");
                Edittext.setSelection(Edittext.getText().toString().length());
            }
        }
    });
if (TextUtils.isEmpty(strBalance)) {
                Toast.makeText(TransactionSellINActivity.this, "Amount should not be empty", Toast.LENGTH_SHORT)
                        .show();
            } else if (strBalance.equalsIgnoreCase("0")) {
                Toast.makeText(TransactionSellINActivity.this, "Amount should not be Zero", Toast.LENGTH_SHORT)
                        .show();
            }
            else if(strBalance.indexOf("0")!=a.length()-1)
                {
                Toast.makeText(TransactionSellINActivity.this, "Amount Not Valid", Toast.LENGTH_SHORT)
                        .show();
                }
                else{
                    Toast.makeText(TransactionSellINActivity.this, "Amount  Valid", Toast.LENGTH_SHORT)
                        .show();
                }

也许对你有用

你想要01或02或者你不想要01或02变得清晰..我不想使用你是否尝试过使用像
startsWith
length
这样的方法我已经更新了答案。看到这个答案你想要01或02或者你不想要01或02变得清晰..我不想使用你是否尝试过使用像
startsWith
length
我已经更新了答案。看到这个答案了吗?什么是mPayEtValue?@Walter抱歉我弄错了什么是mPayEtValue?@Walter抱歉我弄错了这不会在0之后输入数字。是的。非常感谢。0.Yes之后,这将不再是您的电话号码。非常感谢你。
if (TextUtils.isEmpty(strBalance)) {
                Toast.makeText(TransactionSellINActivity.this, "Amount should not be empty", Toast.LENGTH_SHORT)
                        .show();
            } else if (strBalance.equalsIgnoreCase("0")) {
                Toast.makeText(TransactionSellINActivity.this, "Amount should not be Zero", Toast.LENGTH_SHORT)
                        .show();
            }
            else if(strBalance.indexOf("0")!=a.length()-1)
                {
                Toast.makeText(TransactionSellINActivity.this, "Amount Not Valid", Toast.LENGTH_SHORT)
                        .show();
                }
                else{
                    Toast.makeText(TransactionSellINActivity.this, "Amount  Valid", Toast.LENGTH_SHORT)
                        .show();
                }