带正则校验的EditText

这是好久之前写的了,现在整理下文章那就写一下吧,直接上演示Demo,如下所示,伸手党们可直接跳到最后去下载源码。

原理很简单,实现TextWatcher接口中的afterTextChanged()函数,根据正则表达式返回的结果来动态加载rightDrawable,至于校验成功或者失败的drawable用户在初始化的时候可以自定义,下面给出源码参考,其实现了常用的手机、座机、邮箱、url、汉字、用户名的校验,如果你觉得还不满足的话,最后还提供了用户自定义正则的校验,这样总满意了吧,下面给出其代码供参考。

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.widget.EditText;

/**
* Created by cmj on 2016/4/11.
*/
public class AutoCheckEditText extends EditText implements TextWatcher {
//手机校验类型
public static final int TYPE_OF_MOBILE = 0xb0;
//座机校验类型
public static final int TYPE_OF_TEL = 0xb1;
//邮箱校验类型
public static final int TYPE_OF_EMAIL = 0xb2;
//url校验类型
public static final int TYPE_OF_URL = 0xb3;
//汉字校验类型
public static final int TYPE_OF_CHZ = 0xb4;
//用户名校验类型
public static final int TYPE_OF_USERNAME = 0xb5;

//用户自定义
public static final int TYPE_OF_USER_DEFINE = 0xbb;
private int type;
private Drawable successDrawable;
private Drawable unsuccessDrawable;
private String userRegx;

public AutoCheckEditText(Context context) {
super(context);
}

public AutoCheckEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}

public AutoCheckEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}


/**
* @param typ 要校验的类型
* @param success 匹配成功时的图标
* @param unsuccess 匹配失败时的图标
*/
public void creatCheck(int typ, Drawable success, Drawable unsuccess) {
type = typ;
successDrawable = success;
successDrawable.setBounds(0, 0, successDrawable.getMinimumWidth(), successDrawable.getMinimumHeight());
unsuccessDrawable = unsuccess;
unsuccessDrawable.setBounds(0, 0, unsuccessDrawable.getMinimumWidth(), unsuccessDrawable.getMinimumHeight());
this.addTextChangedListener(this);
}

/**
* @param typ 要校验的类型
* @param success 匹配成功时的图标
* @param unsuccess 匹配失败时的图标
*/
public void creatCheck(int typ, Drawable success, Drawable unsuccess, String userRegex) {
creatCheck(typ, success, unsuccess);
this.userRegx = userRegex;
}


@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
System.out.println("a");
}

@Override
public void afterTextChanged(Editable s) {
if (s != null && s.length() > 0) {
boolean isMatch;
if (type == TYPE_OF_MOBILE) {
isMatch = Check.isMobile(s.toString());
} else if (type == TYPE_OF_TEL) {
isMatch = Check.isTel(s.toString());
} else if (type == TYPE_OF_EMAIL) {
isMatch = Check.isEmail(s.toString());
} else if (type == TYPE_OF_URL) {
isMatch = Check.isURL(s.toString());
} else if (type == TYPE_OF_CHZ) {
isMatch = Check.isChz(s.toString());
} else if (type == TYPE_OF_USERNAME) {
isMatch = Check.isUsername(s.toString());
} else if (type == TYPE_OF_USER_DEFINE) {
isMatch = Check.isMatch(userRegx, s.toString());
} else {
return;
}
if (isMatch) {
setCompoundDrawables(null, null, successDrawable, null);
} else {
setCompoundDrawables(null, null, unsuccessDrawable, null);
}
} else {
setCompoundDrawables(null, null, null, null);
}
}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
System.out.println();
}
}

最后献上源码地址:AutoCheckEditTextDemo,欢迎大家star和fork。

打个小广告

欢迎加入我的小专栏「基你太美」一起学习。