Android实战简易教程-第七十枪(自定义实用控制之-邮箱验证EditText)
来源:互联网 发布:oracle和sql语句的区别 编辑:程序博客网 时间:2024/05/17 09:21
我们自定义一款可以验证用户输入邮箱是否符合规范的EditText.
1.布局文件:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="46dp" android:background="@android:color/white" > <EditText android:id="@+id/edittext" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/bg_edittext" android:paddingLeft="50dp" /> <RelativeLayout android:layout_width="46dp" android:layout_height="match_parent" > <ImageView android:id="@+id/image" android:layout_width="22dp" android:layout_height="30dp" android:layout_centerInParent="true" android:scaleType="centerInside" /> </RelativeLayout></RelativeLayout>2.自定义控件:
package com.example.drawableedittext;import java.util.regex.Matcher;import java.util.regex.Pattern;import android.content.Context;import android.text.Editable;import android.text.TextWatcher;import android.util.AttributeSet;import android.view.LayoutInflater;import android.view.View;import android.widget.EditText;import android.widget.ImageView;import android.widget.RelativeLayout;public class DrawableEditText extends RelativeLayout {private Context mContext;private EditText mEditText;private ImageView mImage;public DrawableEditText(Context context, AttributeSet attrs) {super(context, attrs);this.mContext = context;init();// 设置文字大小int textSize = attrs.getAttributeResourceValue(null, "textSize", 0);if (textSize != 0) {mEditText.setTextSize(textSize);}
// 设置edittext的hint提示int hint = attrs.getAttributeResourceValue(null, "hint", 0);if (hint != 0) {mEditText.setHint(hint);}// 设置文本颜色int textColor = attrs.getAttributeResourceValue(null, "textColor", 0);if (textColor != 0) {mEditText.setTextColor(textColor);}}// 初始化布局和控件public void init() {LayoutInflater inflater = LayoutInflater.from(mContext);View view = inflater.inflate(R.layout.drawable_edittext, this);mEditText = (EditText) view.findViewById(R.id.edittext);mImage = (ImageView) view.findViewById(R.id.image);}// 根据文本框是否为空设置不同的图片private void setDrawable() {if (checkEmail(mEditText.getText().toString())) {mImage.setImageResource(R.drawable.right);//mImage.setImageResource(nullImgRes);} else {//mImage.setImageResource(imgRes);mImage.setImageResource(R.drawable.close);}}@Overrideprotected void onFinishInflate() {super.onFinishInflate();// 文本框的text改变监听mEditText.addTextChangedListener(new TextWatcher() {@Overridepublic void onTextChanged(CharSequence s, int start, int before, int count) {}@Overridepublic void beforeTextChanged(CharSequence s, int start, int count, int after) {}@Overridepublic void afterTextChanged(Editable s) {setDrawable();}});}/** * 验证邮箱 * * @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;}}3.引入控件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" > <com.example.drawableedittext.DrawableEditText android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerInParent="true" imgRes="@drawable/phonenumber_click" nullImgRes="@drawable/phonenumber" /></RelativeLayout>运行实例:
0 0
- Android实战简易教程-第七十枪(自定义实用控制之-邮箱验证EditText)
- Android实战简易教程-第七十枪(自定义实用控制之-邮箱验证EditText)
- Android实战简易教程-第三十二枪(自定义View登录注册界面EditText-实现一键清空)
- Android实战简易教程-第三十二枪(自定义View登录注册界面EditText-实现一键清空)
- Android实战简易教程<三十二>(自定义View登录注册界面EditText-实现一键清空)
- Android实战简易教程-第四十六枪(自定义控件体验之罗盘)
- Android实战简易教程-第十枪(画廊组件Gallery实用研究)
- Android实战简易教程-第四十五枪(SlideSwitch-好看又实用的开关按钮)
- Android实战简易教程<四十六>(自定义控件体验之罗盘)
- Android实战简易教程-第三十六枪(监听短信-实现短信验证码自动填入)
- Android实战简易教程-第四十一枪(显示倒计时的Button-适用于获取验证码)
- Android实战简易教程-第三十三枪(自定义View实现控件晃动提示效果)
- Android实战简易教程-第六十五枪(自定义控件实现数字液晶时钟Demo)
- Android实战简易教程-第六十九枪(自定义控件实现雪花飘落效果)
- Android实战简易教程-第六十九枪(自定义控件实现雪花飘落效果)
- Android实战简易教程-第四十二枪(github实用控件推荐BadgeView-图标左上角消息提示控件)
- Android实战简易教程<十>(画廊组件Gallery实用研究)
- Android实战简易教程<四十二>(github实用控件推荐BadgeView-图标左上角消息提示控件)
- 小米开源文件管理器MiCodeFileExplorer-源码研究(9)-入口分析
- MYSQL存储过程
- ios学习笔记(一)基础知识
- VS2013 激活码(注册码,密钥)
- 更新某个字段时触发如何写
- Android实战简易教程-第七十枪(自定义实用控制之-邮箱验证EditText)
- codis linux 部署
- Collection探究之Vector
- 公司防蹭网 ROS配置MAC地址绑定过滤
- C++函数返回值,你必须注意的问题
- 张一鸣与今日头条的理性蜕变
- 反思
- linux uname命令的使用
- 当表某个字段更新是触发器编写