Android:如何验证 EditText 输入?
我需要对一系列 EditText 进行表单输入验证。我使用 OnFocusChangeListeners 在用户输入每个内容后触发验证,但这对于最后一个 EditText 的行为并不理想。
如果我在输入最终的 EditText 时单击“完成”按钮,则 InputMethod 将断开连接,但从技术上讲,焦点永远不会丢失在 EditText 上(因此永远不会发生验证)。
最好的解决方案是什么?
我是否应该监视 InputMethod 何时与每个 EditText 解除绑定,而不是何时监视焦点更改?如果是这样,怎么办?
I need to do form input validation on a series of EditTexts. I'm using OnFocusChangeListeners to trigger the validation after the user types into each one, but this doesn't behave as desired for the last EditText.
If I click on the "Done" button while typing into the final EditText then the InputMethod is disconnected, but technically focus is never lost on the EditText (and so validation never occurs).
What's the best solution?
Should I be monitoring when the InputMethod unbinds from each EditText rather than when focus changes? If so, how?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
为什么不使用
TextWatcher
?由于您有许多
EditText
框需要验证,我认为以下内容适合您:android.text.TextWatcher
接口afterTextChanged(Editable s)
方法,如下所示。Editable s
并不能真正帮助查找哪个 EditText 框的文本正在更改。 直接检查 EditText 框的内容但是您可以像使用相同的方法一样 。我希望我很清楚,如果我很清楚,这会有所帮助! :)
编辑:有关更简洁的方法,请参阅下面的 Christopher Perry 的回答。
Why don't you use
TextWatcher
?Since you have a number of
EditText
boxes to be validated, I think the following shall suit you :android.text.TextWatcher
interfaceafterTextChanged(Editable s)
method as followsThe
Editable s
doesn't really help to find which EditText box's text is being changed. But you could directly check the contents of the EditText boxes likein the same method. I hope I'm clear and if I am, it helps! :)
EDIT: For a cleaner approach refer to Christopher Perry's answer below.
TextWatcher 对我来说有点冗长,所以我做了一些更容易理解的东西:
就像这样使用它:
TextWatcher is a bit verbose for my taste, so I made something a bit easier to swallow:
Just use it like this:
如果您希望在发生错误时得到良好的验证弹出窗口和图像,您可以使用
setError
EditText
类的方法,正如我所描述的此处If you want nice validation popups and images when an error occurs you can use the
setError
method of theEditText
class as I describe here为了减少验证逻辑的冗长性,我编写了一个Android 库。它使用注释和内置规则处理大部分日常验证。有@TextRule、
@NumberRule
、@Required
、@Regex
、@Email等约束
、@IpAddress
、@Password
等,您可以将这些注释添加到 UI 小部件引用中并执行验证。它还允许您异步执行验证,这非常适合从远程服务器检查唯一用户名等情况。
项目主页上有一个关于如何使用注释的示例。您还可以阅读相关博客文章< /a> 我在其中编写了有关如何编写自定义验证规则的示例代码。
这是一个描述该库的用法的简单示例。
该库是可扩展的,您可以通过扩展
Rule
类来编写自己的规则。In order to reduce the verbosity of the validation logic I have authored a library for Android. It takes care of most of the day to day validations using Annotations and built-in rules. There are constraints such as
@TextRule
,@NumberRule
,@Required
,@Regex
,@Email
,@IpAddress
,@Password
, etc.,You can add these annotations to your UI widget references and perform validations. It also allows you to perform validations asynchronously which is ideal for situations such as checking for unique username from a remote server.
There is a example on the project home page on how to use annotations. You can also read the associated blog post where I have written sample codes on how to write custom rules for validations.
Here is a simple example that depicts the usage of the library.
The library is extendable, you can write your own rules by extending the
Rule
class.更新方法 - TextInputLayout:
Google 最近推出了设计支持库,有一个名为 TextInputLayout,它支持通过
setErrorEnabled(boolean)
和setError(CharSequence)
显示错误。如何使用它?
第 1 步:用 TextInputLayout 包装您的 EditText:
第 2 步:验证输入
我已经在我的 Github 存储库,如果您愿意,请查看示例!
Updated approach - TextInputLayout:
Google has recently launched design support library and there is one component called TextInputLayout and it supports showing an error via
setErrorEnabled(boolean)
andsetError(CharSequence)
.How to use it?
Step 1: Wrap your EditText with TextInputLayout:
Step 2: Validate input
I have created an example over my Github repository, checkout the example if you wish to!
这是来自这里
This was nice solution from here
我编写了一个扩展 EditText 的类,它本身支持一些验证方法,并且实际上非常灵活。
目前,正如我所写,通过xml属性验证方法本地支持:
您可以查看 这里
希望你喜欢它:)
I wrote a class that extends EditText which supports natively some validation methods and is actually very flexible.
Current, as I write, natively supported through xml attributes validation methods are:
You can check it out here
Hope you enjoy it :)
我发现 InputFilter 更适合验证 Android 上的文本输入。
这是一个简单的例子:
怎么做我在 Android 中使用 InputFilter 来限制 EditText 中的字符?
您可以添加一个 Toast 来向用户反馈您的限制。
还要检查 android:inputType 标记。
I find InputFilter to be more appropriate to validate text inputs on android.
Here's a simple example:
How do I use InputFilter to limit characters in an EditText in Android?
You could add a Toast to feedback the user about your restrictions.
Also check the android:inputType tag out.
我需要进行字段内验证而不是字段间验证,以测试我的值在一种情况下是无符号浮点值,在另一种情况下是有符号浮点值。这似乎对我有用:
注意,“numberSigned|numberDecimal”内不得有任何空格。例如:“numberSigned | numberDecimal”将不起作用。我不知道为什么。
I needed to do intra-field validation and not inter-field validation to test that my values were unsigned floating point values in one case and signed floating point values in another. Here's what seems to work for me:
Note, you must not have any spaces inside "numberSigned|numberDecimal". For example: "numberSigned | numberDecimal" won't work. I'm not sure why.
这看起来真的很有希望,正是文档为我订购的:
EditText Validator
自定义验证器加上这些内置:
This looks really promising and just what the doc ordered for me:
EditText Validator
custom validators plus these built in:
在 main.xml 文件中,
您可以放置以下属性来验证编辑文本中只能接受字母字符。
这样做:
In main.xml file
You can put the following attrubute to validate only alphabatics character can accept in edittext.
Do this :
当用户点击键盘上的“完成”按钮时,您可以通过监听来获得所需的行为,还可以在我的文章中查看有关使用 EditText 的其他提示 "Android表单验证-正确的方式"
示例代码:
You can get desired behavior by listening when user hit "Done" button on keyboard, also checkout other tips about working with EditText in my post "Android form validation - the right way"
Sample code:
对于电子邮件和密码验证尝试
for email and password validation try
我已经为 android 创建了这个库,您可以在其中轻松验证内部的材料设计 EditText 和 EditTextLayout,如下所示:
然后您可以像这样使用它:
然后您可以像这样检查它是否有效:
有关更多示例和自定义,请检查存储库
https://github.com/TeleClinic/SmartEditText
I have created this library for android where you can validate a material design EditText inside and EditTextLayout easily like this:
then you can use it like this:
Then you can check if it is valid like this:
For more examples and customization check the repository
https://github.com/TeleClinic/SmartEditText