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

前端 未结 10 667
别跟我提以往
别跟我提以往 2020-12-18 21:09

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

相关标签:
10条回答
  • 2020-12-18 21:24

    Please put space between like below string, it working code.

     android:digits="abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ "
    
    0 讨论(0)
  • 2020-12-18 21:30

    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 "";
            }
        }
    });
    
    0 讨论(0)
  • 2020-12-18 21:30

    this worked for me (whitespace in the middle, not at the end) android:digits="abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ"

    0 讨论(0)
  • 2020-12-18 21:31

    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

    0 讨论(0)
  • 2020-12-18 21:33

    use regex.. pattern should be

    Full_Name_Pattern = "[A-Z][a-z]+( [A-Z][a-z]+)*";
    
    0 讨论(0)
  • 2020-12-18 21:35

    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.

    0 讨论(0)
提交回复
热议问题