How to create EditText accepts Alphabets only in a

2019-01-03 15:00发布

How can I enter only alphabets in EditText in android?

10条回答
ゆ 、 Hurt°
2楼-- · 2019-01-03 15:46

Allow only Alphabets in EditText android:

InputFilter letterFilter = new InputFilter() {
        public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
            String filtered = "";
            for (int i = start; i < end; i++) {
                char character = source.charAt(i);
                if (!Character.isWhitespace(character)&&Character.isLetter(character)) {
                    filtered += character;
                }
            }

            return filtered;
        }

    };
editText.setFilters(new InputFilter[]{letterFilter}); 
查看更多
干净又极端
3楼-- · 2019-01-03 15:47

If anybody still wants this, Java regex for support Unicode? is a good one. It's for when you want ONLY letters (no matter what encoding - japaneese, sweedish) iside an EditText. Later, you can check it using Matcher and Pattern.compile()

查看更多
我命由我不由天
4楼-- · 2019-01-03 15:49

Put code edittext xml file,

   android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
查看更多
看我几分像从前
5楼-- · 2019-01-03 15:56

For spaces you can add single space in the digits. If you need any special characters like dot, comma also you can to this list

android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ "

查看更多
登录 后发表回答