【问题标题】:How to restrict entry of numbers but allow all the special characters and alphabets in EditText?如何限制输入数字但允许 EditText 中的所有特殊字符和字母?
【发布时间】:2018-05-18 09:20:38
【问题描述】:

如何限制所有数字输入,但允许 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" />

【问题讨论】:

    标签: android android-layout android-edittext


    【解决方案1】:

    创建一个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});
    

    【讨论】:

      【解决方案2】:

      您可以使用这个Android-Validator 库并使用正则表达式来验证输入。有很多内置的验证器,或者你可以自己写

      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.
      

      你会得到这个

      【讨论】:

        【解决方案3】:

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

        【讨论】:

          猜你喜欢
          • 2019-12-15
          • 1970-01-01
          • 2021-01-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多