Android 编辑文本验证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1151664/
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
Validation on Edit Text
提问by Kumar
I would like to know how to make a validation on EditText. For example, I have one EditText that should only accept numeric values. If something other than a numeric value is typed by the user then it should show an alert message (i.e."please use a numeric value....").
我想知道如何对 EditText 进行验证。例如,我有一个只应接受数字值的 EditText。如果用户键入的不是数字值,那么它应该显示一条警告消息(即“请使用数字值....”)。
Is there a function available to find out if the entered text is particular type? If possible please include a code snippet.
是否有可用的功能来确定输入的文本是否为特定类型?如果可能,请包含代码片段。
回答by Will
Rather than make a pop-up I would integrate a hint into the EditText and I would make it so the user could only type numbers into the EditText (android:numeric, android:hint):
我不会弹出一个提示,而是将一个提示集成到 EditText 中,我会这样做,以便用户只能在 EditText 中键入数字(android:numeric, android:hint):
<EditText android:layout_height="wrap_content"
android:numeric="integer"
android:hint="@string/numberHint"
android:gravity="left"
android:id="@+id/name"
android:layout_width="wrap_content"
android:maxWidth="60dp"
android:textSize="6pt">
</EditText>
More information is available here: http://developer.android.com/reference/android/widget/EditText.html
此处提供更多信息:http: //developer.android.com/reference/android/widget/EditText.html
回答by Piyush Patel
Another way , editText.setInputType(InputType.TYPE_CLASS_NUMBER);
另一种方式, editText.setInputType(InputType.TYPE_CLASS_NUMBER);
Please Go through My Blog post on android input validation [updated].
请浏览我的博客文章关于 android 输入验证 [更新]。
EDIT:
编辑:
Which has information on,
其中有信息,
- What is regular expression
- How to validate android edittext input
- Online regular expression library
- Online regular expression testing tool
- Learn how to write regular expression
- 什么是正则表达式
- 如何验证android edittext输入
- 在线正则表达式库
- 在线正则表达式测试工具
- 学习如何编写正则表达式
回答by Jaavaaan
The default capabilities for text/checkbox etc validation is poor within android. I have written some supporting classes to fix this. It contains a validator interface, an abstract inplementation,a validationresult class and 2 examples of custom implemented validations. 1 for regular expressions on text and a simple one to check if a checkbox is checked.
android 中文本/复选框等验证的默认功能很差。我已经编写了一些支持类来解决这个问题。它包含一个验证器接口、一个抽象实现、一个验证结果类和 2 个自定义实现的验证示例。1 用于文本上的正则表达式,还有一个用于检查复选框是否被选中的简单表达式。
Here is the link to my blog containing the sources and a small bit of explaining Form validation on Android
这是我的博客的链接,其中包含源代码和对 Android 上的表单验证的一些解释
回答by Donn Felker
If you want nice looking validation messages you can use the setError method on the EditText control as I show here: http://blog.donnfelker.com/2011/11/23/android-validation-with-edittext/
如果您想要漂亮的验证消息,您可以在 EditText 控件上使用 setError 方法,如我在此处所示:http: //blog.donnfelker.com/2011/11/23/android-validation-with-edittext/