Android:如何在 EditText 中设置可接受的数字和字符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10344493/
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
Android : How to set acceptable numbers and characters in EditText?
提问by iamtheexplm06
I have to set acceptable characters "0123456789" and "semicolon" in the EditText. Below is the code I'm using.
我必须在 EditText 中设置可接受的字符“0123456789”和“分号”。下面是我正在使用的代码。
android:digits="0123456789;"
android:inputType="number|text
The problem with that implementation is in HTC phones, semicolon can't be entered but in Samsung and Sony Ericsson, semicolon can be entered. Another problem is when I entered semicolon in Samsung and Sony Ericsson, semicolon can't be deleted. Is there any missing property in the above code? Thanks in advance.
该实现的问题是在 HTC 手机中,不能输入分号,但在三星和索尼爱立信中,可以输入分号。还有一个问题是我在三星和索尼爱立信输入分号时,分号不能删除。上面的代码中是否缺少任何属性?提前致谢。
回答by Shankar Agarwal
Android provides an easy way to edit text fields by modifying the layout xml and adding an android:inputType="text". This lets you easily create some basic validations like numbers, decimal, phone or emails. But there's no parameter for alphanumeric (i.e. no special characters). To do this, you need to use an input filter like below and set the fields you want to validate with that filter in code. This input filter
Android 通过修改布局 xml 并添加 android:inputType="text" 提供了一种编辑文本字段的简单方法。这使您可以轻松创建一些基本验证,如数字、十进制、电话或电子邮件。但是没有字母数字参数(即没有特殊字符)。为此,您需要使用如下所示的输入过滤器,并在代码中使用该过滤器设置要验证的字段。这个输入过滤器
InputFilter alphaNumericFilter = new InputFilter() {
@Override
public CharSequence filter(CharSequence arg0, int arg1, int arg2, Spanned arg3, int arg4, int arg5)
{
for (int k = arg1; k < arg2; k++) {
if (!Character.isLetterOrDigit(arg0.charAt(k))) {
return "";
} //the first editor deleted this bracket when it is definitely necessary...
}
return null;
}
};
mFirstName.setFilters(new InputFilter[]{ alphaNumericFilter});
回答by Sam
To limit what the user can enter as they type use a TextWatcher, discussed in this question Android: How can I validate EditText input?.
要限制用户在键入时可以输入的内容,请使用 TextWatcher,在这个问题Android: How can I validate EditText input? 中讨论。
Better yet: Allow only selected charcters based on regex in an EditText
回答by JoxTraex
The solution is to modify the these fields: http://developer.android.com/reference/android/widget/TextView.html#attr_android:inputMethod
解决方法是修改这些字段:http: //developer.android.com/reference/android/widget/TextView.html#attr_android: inputMethod
Set the InputMethod of the EditText so that you can properly get what you need.
设置 EditText 的 InputMethod 以便您可以正确获取所需内容。
回答by chris10169
Here is the regular expression
这是正则表达式
Pattern mPattern = Pattern.compile("^([1-9][0-9]{0,2})?(\.[0-9]?)?$");
Matcher matcher = mPattern.matcher(loginNameString.toString());
if(!matcher.find())
{
//TODO
}