Text Only Input EditText android

痴心易碎 提交于 2019-12-11 10:06:47

问题


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 ?


回答1:


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 :)




回答2:


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 "";
            }
        } });



回答3:


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



来源:https://stackoverflow.com/questions/4265377/text-only-input-edittext-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!