问题描述
我想验证的EditText,看看是否所有的字母是字母。它应该能够处理多个的EditText。我需要进入的任何其他然后字母的方式来prevent用户。
i wanted to validate a EditText and see if all of the letters are alphabet. it should be able to handle multiple EditText. i need a way to prevent user from entering anything other then alphabet.
userTextInput=(EditText)findViewById(R.id.textInput);
userTextInput.addTextChangedListener(this);
public void afterTextChanged(Editable edit) {
String textFromEditView = edit.toString();
//first method
ArrayList[] ArrayList;
//ArrayList [] = new ArrayList;
for(int i=0; i<=textFromEditView.length(); i++)
{
if(textFromEditView[i].isLetter() == false)
{
edit.replace(0, edit.length(), "only alphabets");
}
}
//second method
try
{
boolean isOnlyAlphabet = textFromEditView.matches("/^[a-z]+$/i");
if(isOnlyAlphabet == false)
{
edit.replace(0, edit.length(), "only alphabets");
}
}
catch(NumberFormatException e){}
}
我的第二个方法的那一刻我输入任何内容,数字或字母,我的应用程序崩溃。
我有我的测试第一种方法,因为它有错误textFromEditView必须是一个数组类型积屑瘤解析为字符串。你能帮助我提高我的code。
for my second method the moment i enter anything, number or alphabet, my app crash.i have test my first method because it has the error textFromEditView must be an array type bue is resolved as string. can you help me to improve my code.
推荐答案
我想你会做的更好使用的
I think you'd do better using InputFilter:
InputFilter filter = new InputFilter() {
public CharSequence filter(CharSequence src, int start, int end,
Spanned d, int dstart, int dend) {
for (int i = start; i < end; i++) {
if (!Character.isLetter(src.charAt(i))) {
return src.subSequence(start, i-1);
}
}
return null;
}
};
edit.setFilters(new InputFilter[]{filter});
code的修改:How我用输入过滤器在Android的一个EditText限制字符?
这篇关于Android的字母的EditText只验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!