如何创建 EditText 仅在 android 中接受字母?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2361497/
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
How to create EditText accepts Alphabets only in android?
提问by UMAR
How can I enter only alphabets in EditText in android?
如何在android的EditText中只输入字母?
采纳答案by UMAR
EditText state = (EditText) findViewById(R.id.txtState);
Pattern ps = Pattern.compile("^[a-zA-Z ]+$");
Matcher ms = ps.matcher(state.getText().toString());
boolean bs = ms.matches();
if (bs == false) {
if (ErrorMessage.contains("invalid"))
ErrorMessage = ErrorMessage + "state,";
else
ErrorMessage = ErrorMessage + "invalid state,";
}
回答by Sandeep
Add this line with your EditText tag.
将此行与您的 EditText 标记一起添加。
android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
Your EditText tag should look like:
您的 EditText 标签应如下所示:
<EditText
android:id="@+id/editText1"
android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
回答by Subhas
edittext.setFilters(new InputFilter[] {
new InputFilter() {
public CharSequence filter(CharSequence src, int start,
int end, Spanned dst, int dstart, int dend) {
if(src.equals("")){ // for backspace
return src;
}
if(src.toString().matches("[a-zA-Z ]+")){
return src;
}
return edittext.getText().toString();
}
}
});
please test thoroughly though !
请彻底测试!
回答by swetabh suman
For those who want that their editText should accept only alphabets and space (neither numerics nor any special characters), then one can use this InputFilter
.
Here I have created a method named getEditTextFilter()
and written the InputFilter inside it.
对于那些希望他们的 editText 只接受字母和空格(既不是数字也不是任何特殊字符)的人,那么可以使用 this InputFilter
。在这里,我创建了一个名为getEditTextFilter()
InputFilter的方法,并在其中写入了 InputFilter。
public static InputFilter getEditTextFilter() {
return new InputFilter() {
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
boolean keepOriginal = true;
StringBuilder sb = new StringBuilder(end - start);
for (int i = start; i < end; i++) {
char c = source.charAt(i);
if (isCharAllowed(c)) // put your condition here
sb.append(c);
else
keepOriginal = false;
}
if (keepOriginal)
return null;
else {
if (source instanceof Spanned) {
SpannableString sp = new SpannableString(sb);
TextUtils.copySpansFrom((Spanned) source, start, sb.length(), null, sp, 0);
return sp;
} else {
return sb;
}
}
}
private boolean isCharAllowed(char c) {
Pattern ps = Pattern.compile("^[a-zA-Z ]+$");
Matcher ms = ps.matcher(String.valueOf(c));
return ms.matches();
}
};
}
Attach this inputFilter to your editText after finding it, like this :
找到后将此 inputFilter 附加到您的 editText ,如下所示:
mEditText.setFilters(new InputFilter[]{getEditTextFilter()});
The original credit goes to @UMAR who gave the idea of validating using regular expression and @KamilSeweryn
最初的功劳归功于@UMAR,他提出了使用正则表达式和@KamilSeweryn 进行验证的想法
回答by Naveed Ashraf
Through Xml you can do easily as type following code in xml (editText)...
通过 Xml,您可以轻松地在 xml (editText) 中键入以下代码...
android:digits="abcdefghijklmnopqrstuvwxyz"
only characters will be accepted...
只接受字符...
回答by Najib Ahmed Puthawala
Put code edittext xml file,
把代码edittext xml文件,
android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
回答by Naveen Kumar M
For spaces, you can add single space in the digits. If you need any special characters like the dot, a comma also you can add to this list
对于空格,您可以在数字中添加一个空格。如果您需要任何特殊字符,例如点,逗号也可以添加到此列表中
android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ "
android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
回答by Umar Waqas
Allow only Alphabets in EditText android:
在 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});
回答by Vinayak Bevinakatti
回答by mdzeko
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()
如果有人仍然想要这个,Java regex 支持 Unicode?是一个很好的。当您只需要在 EditText 旁边的字母(无论什么编码 - 日语,瑞典语)时。稍后,您可以使用Matcher
和检查它Pattern.compile()