如何在Android中的EditText中只输入字母?
答案 0 :(得分:148)
使用EditText标记添加此行。
android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
您的EditText标记应如下所示:
<EditText
android:id="@+id/editText1"
android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
答案 1 :(得分:55)
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 "";
}
}
});
请尽快测试!
答案 2 :(得分:21)
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,";
}
答案 3 :(得分:19)
对于那些希望他们的editText只接受字母和空格(无数字或任何特殊字符)的人,可以使用此InputFilter
。
在这里,我创建了一个名为getEditTextFilter()
的方法,并在其中编写了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();
}
};
}
找到后将此inputFilter附加到editText,如下所示:
mEditText.setFilters(new InputFilter[]{getEditTextFilter()});
最初的功劳归于@UMAR,他提出了使用正则表达式和@KamilSeweryn进行验证的想法
答案 4 :(得分:18)
通过Xml,您可以轻松地按照xml(editText)中的代码进行操作...
android:digits="abcdefghijklmnopqrstuvwxyz"
只会接受字符...
答案 5 :(得分:7)
放置代码edittext xml文件,
android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
答案 6 :(得分:5)
仅允许EditText中的字母表安卓:
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});
答案 7 :(得分:4)
对于空格,您可以在数字中添加单个空格。如果您需要任何特殊字符,如点,逗号也可以添加到此列表
android:digits =“abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ”
答案 8 :(得分:1)
试试这个
<EditText
android:id="@+id/EditText1"
android:text=""
android:inputType="text|textNoSuggestions"
android:textSize="18sp"
android:layout_width="80dp"
android:layout_height="43dp">
</EditText>
可以找到其他inputType Here ..
答案 9 :(得分:0)
如果有人仍然想要这个,Java regex for support Unicode?是个好人。当你想要只有字母(无论是什么编码 - japaneese,瑞典语)时,它就是一个EditText。之后,您可以使用Matcher
和Pattern.compile()
答案 10 :(得分:0)
试试这个方法
对于 Java:
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 "";
}
}});
对于 Kotlin:
val yourEditText = findViewById<View>(android.R.id.yourEditText) as EditText
val reges = Regex("^[0-9a-zA-Z ]+$")
//this will allow user to only write letter and white space
yourEditText.filters = arrayOf<InputFilter>(
object : InputFilter {
override fun filter(
cs: CharSequence, start: Int,
end: Int, spanned: Spanned?, dStart: Int, dEnd: Int,
): CharSequence? {
if (cs == "") { // for backspace
return cs
}
return if (cs.toString().matches(reges)) {
cs
} else ""
}
}
)