Android 表单验证 UI 库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11776829/
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 form validation UI library
提问by Anastasia
There is iOS US2FormValidatorlibrary for user input validation (see the picture below). I think that library is better than the default of just popping an alert when something doesn't validate.
有用于用户输入验证的iOS US2FormValidator库(见下图)。我认为该库比在某些内容未验证时弹出警报的默认设置要好。
I'm looking for how to do such things on Android. Are there some Android analogs of US2FormValidator?
我正在寻找如何在 Android 上做这样的事情。是否有US2FormValidator 的一些 Android 类似物?
回答by Ragunath Jawahar
The pop-up effect you have shown on your screenshot can be achieved using Android's built-in setError(String)
method on EditText
widgets.
您在屏幕截图上显示的弹出效果可以使用 Android 的小部件内置setError(String)
方法来实现EditText
。
Also, you can leverage the power of annotations using the Android Saripaarlibrary that I've authored.
此外,您可以使用我编写的Android Saripaar库来利用注释的强大功能。
first add the library:
首先添加库:
compile 'com.mobsandgeeks:android-saripaar:2.0.2'
The library is very simple to use. In your activity annotate the View
references you would like to validate as in the following example.
该库使用起来非常简单。在您的活动中注释View
您要验证的引用,如下例所示。
@Order(1)
private EditText fieldEditText;
@Order(2)
@Checked(message = "You must agree to the terms.")
private CheckBox iAgreeCheckBox;
@Order(3)
@Length(min = 3, message = "Enter atleast 3 characters.")
@Pattern(regex = "[A-Za-z]+", message = "Should contain only alphabets")
private TextView regexTextView;
@Order(4)
@Password(min = 6, scheme = Password.Scheme.ALPHA_NUMERIC_MIXED_CASE_SYMBOLS)
private EditText passwordEditText;
@Order(5)
@ConfirmPassword
private EditText confirmPasswordEditText;
The order
attribute specifies the order in which the fields have to be validated.
该order
属性指定必须验证字段的顺序。
In your onCreate()
method instantiate a new Validator
object. and call validator.validate()
inside any of your event listeners.
在您的onCreate()
方法中实例化一个新Validator
对象。并validator.validate()
在您的任何事件侦听器中调用。
You'll receive callbacks on onSuccess
and onFailure
methods of the ValidationListener
.
您将收到onSuccess
和onFailure
方法的回调ValidationListener
。
If you want to show a pop-up as show in the image above then do the following,
如果要显示如上图所示的弹出窗口,请执行以下操作,
public void onValidationFailed(View failedView, Rule<?> failedRule) {
if (failedView instanceof Checkable) {
Toast.makeText(this, failedRule.getFailureMessage(), Toast.LENGTH_SHORT).show();
} else if (failedView instanceof TextView) {
TextView view = (TextView) failedView;
view.requestFocus();
view.setError(failedRule.getFailureMessage());
}
}
Hope that helps.
希望有帮助。
回答by Sharafat Mollah Mosharraf
Android has extremely easy-to-use built-in validation mechanism which is great enough. See the following link: http://blog.donnfelker.com/2011/11/23/android-validation-with-edittext/
Android 具有非常易于使用的内置验证机制,这已经足够了。请参阅以下链接:http: //blog.donnfelker.com/2011/11/23/android-validation-with-edittext/
回答by kassim
I've just come across ValidationKomensky that you may find useful
我刚刚遇到了 ValidationKomensky,你可能会觉得它很有用
https://github.com/inmite/android-validation-komensky
https://github.com/inmite/android-validation-komensky