Text Only Input EditText android

2019-07-22 14:29发布

I want to restric user from entering any special character or number into edit text, but programmatically it should allow.

I know I can make inputType="text" but it deals only with the softkeyboard. I think it can be achived with TextWatcher, but I am not able to gel evrything. Can anyone help ?

3条回答
\"骚年 ilove
2楼-- · 2019-07-22 14:54
editTextView.setInputType(InputType.TYPE_TEXT_VARIATION_NORMAL);

There are many other types of inputs available for Password, Email address etc. Please see This API Link

Thanks :)

查看更多
趁早两清
3楼-- · 2019-07-22 14:55

Try this way,Reference

android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
android:inputType="text"

For more info, you may check http://developer.android.com/reference/android/widget/TextView.html#attr_android:digits

查看更多
Bombasti
4楼-- · 2019-07-22 15:05

You can filter input text by following ,

etTwamboxName.setFilters(new InputFilter[] { new InputFilter() {
            public CharSequence filter(CharSequence src, int start, int end,
                    Spanned dst, int dstart, int dend) {

                Log.e("", "===> " + src);
                if (src.equals("")) { // for backspace
                    return src;
                }

                if (src.toString().matches("[a-zA-Z ]+.")) {
                    return src;
                }

                return "";
            }
        } });
查看更多
登录 后发表回答