奶霸000

导航

android 自定义view -- 实现自定义 邮箱验证的Edittext

//onFinishInflate 当View中所有的子控件均被映射成xml后触发   



/**
* 实现自定义 实现邮箱验证的EidtText
*/
public class CustomEditText extends RelativeLayout {

private Context context;
private EditText mEditText;
private ImageView mImage;

public CustomEditText(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
inint();


TypedArray typeArray = context.obtainStyledAttributes(attrs, R.styleable.CustomEditText);

int textColor = typeArray.getColor(R.styleable.CustomEditText_textColors, 0);
String hintText = typeArray.getString(R.styleable.CustomEditText_hint);
int textSize = typeArray.getInt(R.styleable.CustomEditText_textSizes, 10);
LogUtils.logE(hintText);
mEditText.setTextColor(textColor);
mEditText.setHint(hintText);
mEditText.setTextSize(textSize);
typeArray.recycle();

}

private void inint() {
//设置字体的大小
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.view_custom_eidttext, this);
mEditText = (EditText) view.findViewById(R.id.edittext);
mImage = (ImageView) view.findViewById(R.id.image);
}


//onFinishInflate 当View中所有的子控件均被映射成xml后触发
@Override
protected void onFinishInflate() {
super.onFinishInflate();
// 文本框的text改变监听
mEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//在text 改变之执行

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
//在text 正在改变的时候执行

}

@Override
public void afterTextChanged(Editable s) {
//在text 改变之后执行
setDrawable();
LogUtils.logE(s.toString());
}
});
}

// 根据文本框是否为空设置不同的图片
private void setDrawable() {
if (checkEmail(mEditText.getText().toString())) {
mImage.setImageResource(R.mipmap.pass);
//mImage.setImageResource(nullImgRes);
} else {
//mImage.setImageResource(imgRes);
mImage.setImageResource(R.mipmap.close);
}
}

/**
* 验证邮箱
*
* @param email
* @return
*/
public static boolean checkEmail(String email) {
boolean flag = false;
try {
String check = "^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
Pattern regex = Pattern.compile(check);
Matcher matcher = regex.matcher(email);
flag = matcher.matches();
} catch (Exception e) {
flag = false;
}
return flag;
}
}



//attrs 文件
<resources>
<declare-styleable name="CustomEditText">
<attr name="textSizes" format="integer"></attr>
<attr name="textColors" format="color|reference"></attr>
<attr name="text" format="string"></attr>
<attr name="hint" format="string"></attr>
</declare-styleable>
</resources>

//布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:zhy="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<newdemo.jeno.designdemo.view.CustomEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
zhy:textColors="@android:color/holo_red_dark"
zhy:textSizes="20">


</newdemo.jeno.designdemo.view.CustomEditText>
</LinearLayout>








posted on 2016-09-18 10:09  奶霸000  阅读(660)  评论(0编辑  收藏  举报