当零是第一个数字时,让EditText输入零

XX_*_*her 4 android android-edittext

现在,我的EditText只能输入数字.但是我想禁止在零是第一个数字时输入零.我能怎么做?

Man*_*oba 6

这应该可以帮到你:

        yourTextEdit.addTextChangedListener(new TextWatcher(){
        public void onTextChanged(CharSequence s, int start, int before, int count)
        {
            if (yourTextEdit.getText().matches("^0") )
            {
                // Not allowed
                yourTextEdit.setText("");
            }
        }
        public void beforeTextChanged(CharSequence s, int start, int count, int after){}
        public void afterTextChanged(Editable s){}
    }); 
Run Code Online (Sandbox Code Playgroud)