我想验证一个EditText并查看是否所有的字母都是字母表。它应该能够处理多个EditText。我需要一种方法来防止用户输入任何其他字母。android EditText只字母验证
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必须是数组类型bue被解析为字符串。你能帮我改进我的代码吗?
我发现了一个很好的解决方案使用。 android:digits =“qwertzuiopasdfghjklyxcvbnmQWERTZUIOPASDFGHJKLYXCVBNM”/>在xml中。迄今没有问题。是你给我的同一个链接。谢谢您的帮助! – Myst
看到了,但从未尝试过。如果您对此更满意,那么请不要犹豫,将其作为答案! –