简体   繁体   中英

Making an EditText field accept only letters and white spaces in Android

I have an EditText field in my project which stands for the full name of the person.So I want only letters and spaces to be allowed in it.So I tried the following in the XML file

android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ "

But it didn't work.Can anyone tell me how to do it?

Try this:

EditText yourEditText = (EditText) findViewById(R.id.yourEditText);
yourEditText.setFilters(new InputFilter[] {
    new InputFilter() {
        @Override
        public CharSequence filter(CharSequence cs, int start,
                    int end, Spanned spanned, int dStart, int dEnd) {
            // TODO Auto-generated method stub
            if(cs.equals("")){ // for backspace
                 return cs;
            }
            if(cs.toString().matches("[a-zA-Z ]+")){
                 return cs;
            }
            return "";
        }
    }
});

Below change worked for me:-

android:digits="abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ"

Put space in between your strings. If you put space at the end of a string it will get trimmed off automatically.

I have used space after ending of small letters and starting with capital letters.

Add this line in your EditText tag.

android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

Your EditText tag should look like:

<EditText
        android:id="@+id/edt"
        android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

it works perfectly and no validation condition required


update 1

regex src.toString().matches("[a-zA-Z ]+")


update 2

Fair enough if that's the case then simply add space in between.

android:digits="abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ"

For some reason the inclusion of "\\n" can not occur at the end of the string, I searched the Google documentation, most found nothing. the only way to achieve spaces and line break is including in the middle of the string. See the example I used in one of my apps.

android:digits="aãâáàbcçdeéfghiíjklmnoõôóòpqrstuvwxyzAÃÂÁÀBCÇDEÉFGHIÍJKLMNOÕÔÓÒPQRSTUVWXYZ1234567890@-_'+=(){}[]*%$#!?,.;\n: /?\\"

Try this

android:digits="abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ"

Put space in between your strings. If you put space at the end of a string it will be automatically trimmed off.

I have used space after ending of small letters and starting of capital letters

请在下面的字符串之间放置空格,它是工作代码。

 android:digits="abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ "

Try this solution, it block entering number, special characters except space.

editText.setFilters(new InputFilter[] {
   new InputFilter() {
   @Override
   public CharSequence filter(CharSequence cs, int start,
       int end, Spanned spanned, int dStart, int dEnd) {

   return cs.toString().replaceAll("[^a-zA-Z ]*","");
    }
    }
});

Kotlin Version:

editText.filters = arrayOf(
            InputFilter { source, start, end, dest, dstart, dend ->
                return@InputFilter source.replace(Regex("[^a-zA-Z ]*"), "")
            }
        )

使用正则表达式 .. 模式应该是

Full_Name_Pattern = "[A-Z][a-z]+( [A-Z][a-z]+)*";

在您的layout.xml使用以下layout.xml

<EditText android:inputType="textPersonName" />

这对我有用(中间的空格,而不是最后) android:digits="abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM