如何限制数字输入但允许EditText中的所有特殊字符和字母?

时间:2018-05-18 09:20:39

标签: android android-layout android-edittext

如何限制所有数字输入但允许在android中的editText中的所有特殊字符和字母表。我搜索但没有得到我想要的东西。

 <EditText
                    android:id="@+id/country"
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:layout_centerHorizontal="true"
                    android:layout_margin="8dp"
                    android:drawablePadding="10dp"
                    android:hint="Enter your country"                        
             android:inputType="textVisiblePassword|textNoSuggestions|text"
                    android:textColor="@color/editTextColor"
                    android:textColorHint="@color/hint_color"
                    android:textSize="16dp" />

3 个答案:

答案 0 :(得分:2)

创建InputFilter

EditText editText=findViewById(R.id.et);
    InputFilter filter = new InputFilter() {
        public CharSequence filter(CharSequence source, int start, int end,
                                   Spanned dest, int dstart, int dend) {
            for (int i = start; i < end; i++) {
                if (Character.isDigit(source.charAt(i))) {
                    return "";
                }
            }
            return null;
        }
    };
    editText.setFilters(new InputFilter[]{filter});

答案 1 :(得分:0)

您可以使用此Android-Validator库并使用Regex验证输入。有很多内置验证器,或者你可以写你自己的

Validate usernameValid = new Validate(editUsername);
RegExpValidator userNameRegex = new RegExpValidator(mContext, R.string.validator_string_contain_number);
userNameRegex.setPattern("/^([^0-9]*)$/"); // Regular Expression for String not contain numbers.

你会得到这个 enter image description here

答案 2 :(得分:0)

如果你想在中使用android:digits=""而不使用正则表达式。包括您要添加的所有字符