打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
AValidations首页、文档和下载

AValidations 是 一个免费的、开源的、简易的、遵循Apache Licence 2.0开源协议发布,超级容易扩展的android表单验证的小框架

使用方法:

1.下载zip或者克隆AValidations项目
2.导入Eclipse,右键工程->preference->Android->library->Add,选择AValidations工程加入后 apply应用
3.继承ValidationExecutor写出自己的校验器:

1
2
3
4
5
6
7
8
9
10
11
12
public class UserNameValidation extends ValidationExecutor {
    public boolean doValidate(Context context, String text) {
        String regex = "^[a-zA-Z](?=.*?[a-zA-Z])(?=.*?[0-9])[a-zA-Z0-9_]{7,11}$";
        boolean result = Pattern.compile(regex).matcher(text).find();
        if (!result) {
            Toast.makeText(context, context.getString(R.string.e_username_hint), Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }
}

4.使用EditTextValidator进行校验:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
public class LoginActivity extends Activity implements OnClickListener{
    private EditText usernameEditText;
    private EditText passwordEditText;
    private Button loginButton;
    private EditTextValidator editTextValidator;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        usernameEditText = (EditText) findViewById(R.id.login_username_edittext);
        passwordEditText = (EditText) findViewById(R.id.login_password_edittext);
        loginButton = (Button) findViewById(R.id.login_button);
        loginButton.setOnClickListener(this);
        editTextValidator = new EditTextValidator(this)
            .setButton(loginButton)
            .add(new ValidationModel(usernameEditText,new UserNameValidation()))
            .add(new ValidationModel(passwordEditText,new PasswordValidation()))
            .execute();
    }
    @Override
        public void onClick(View v) {
            switch (v.getId()) {
            case R.id.login_button:
                if (editTextValidator.validate()) {
                    Toast.makeText(this"通过校验", Toast.LENGTH_SHORT).show();
                }
                break;
            }
        }

5.如果需要实现没有填写表单时表单提交按钮不可点击效果 需要设置setButton(view)和写Button背景的selector,如:

1
2
3
4
5
6
7
8
9
10
11
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/red_btn_normal" android:state_focused="true" android:state_pressed="false"/>
<item android:drawable="@drawable/red_btn_selected" android:state_focused="true" android:state_pressed="true"/>
<item android:drawable="@drawable/red_btn_selected" android:state_focused="false" android:state_pressed="true"/>
<item android:drawable="@drawable/red_btn_disable" android:state_enabled="false"/>
<item android:drawable="@drawable/red_btn_normal"/>
</selector>
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
2.5.9 AlertDialog(对话框)详解 | 菜鸟教程
Android中利用httpclient进行网络通信的方法(以用户登录为例说明)
android之实现底部TabHost
[UI控件问题] android屏幕自适应 android 属性
Android 之 设置EditText最大可输入字符
Android开发笔记(五)— 更改手机窗口底色
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服