-2

在我想要使用的应用程序EditText中,为此我应该为此设置限制EditText
在我的EditText JUST 支持字母表(az, AZ) 中,为此我编写了以下代码:

registerUsernameEdtTxt.addTextChangedListener(new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        registerUsernameEdtTxt.setFilters(new InputFilter[]{new InputFilter() {
            public CharSequence filter(CharSequence src, int start,
                                       int end, Spanned dst, int dstart, int dend) {
                if (src != null && src.length() > 0 && dstart == 0) {
                    if (src.toString().matches("[a-zA-Z ].*")) {
                        registerUsernameInptLay.setErrorEnabled(false);
                        return src;
                    }
                } else {
                    registerUsernameInptLay.setError(context.getResources().getString(R.string.insertJustEnglish));
                }
                return "";
            }
        }});
    }

XML 代码:

<android.support.design.widget.TextInputLayout
                                android:id="@+id/registerUsernameInptLay"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:layout_below="@+id/registerUserNameErrTxt"
                                app:counterEnabled="true"
                                app:counterMaxLength="20">

                                <EditText
                                    android:id="@+id/registerUsernameEdtTxt"
                                    android:layout_width="match_parent"
                                    android:layout_height="wrap_content"
                                    android:fontFamily="sans-serif"
                                    android:hint="@string/registerUsername"
                                    android:inputType="text"
                                    android:textColor="@color/darkBlueGrey"
                                    android:textSize="@dimen/font16" />
                            </android.support.design.widget.TextInputLayout>

但是当写字符时,我会看到这个错误:

   <string name="insertJustEnglish">Username can only start with alphabet characters.</string>

我该如何解决?

4

1 回答 1

0

把它写在你的 EditText 标签中

android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
于 2018-05-10T08:24:57.550 回答