Java 使 EditText 字段仅接受 Android 中的字母和空格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26057279/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 01:46:13  来源:igfitidea点击:

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

javaandroid

提问by Achuthan M

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 XMLfile

我的项目中有一个 EditText 字段,它代表此人的全名。所以我只希望其中允许使用字母和空格。所以我在XML文件中尝试了以下操作

android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ "

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

但是没有用。谁能告诉我怎么做?

采纳答案by Batuhan Co?kun

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

回答by Ankit Kumar

use regex.. pattern should be

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

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

回答by Maveňツ

Add this line in your EditText tag.

在 EditText 标签中添加这一行。

android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

Your EditText tag should look like:

您的 EditText 标签应如下所示:

<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

更新 1

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

正则表达式 src.toString().matches("[a-zA-Z ]+")



update 2

更新 2

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

如果是这种情况,那么只需在两者之间添加空间就足够了。

android:digits="abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ"

android:digits="abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ"

回答by Zuop

Use the following in your in your layout.xml.

在您的layout.xml.

<EditText android:inputType="textPersonName" />

回答by Julio Borges

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.

由于某种原因,字符串末尾不能包含“\n”,我搜索了谷歌文档,大部分都没有找到。实现空格和换行的唯一方法是在字符串中间包含。请参阅我在我的一个应用程序中使用的示例。

android:digits="a?aáàbc?deéfghiíjklmno??óòpqrstuvwxyzA??áàBC?DEéFGHIíJKLMNO??óòPQRSTUVWXYZ1234567890@-_'+=(){}[]*%$#!?,.;\n: /?\"

回答by Pratik Sinha

Try this

尝试这个

android:digits="abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ"

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

我在小写字母结尾和大写字母开头后使用了空格

回答by Lorenzo Mreg

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

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

回答by Aditay Kaushal

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

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

 android:digits="abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ "

回答by Tanu

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.

我在小写字母结尾并以大写字母开头后使用了空格。