我们自定义一款可以验证用户输入邮箱是否符合规范的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>
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); } } @Override protected void onFinishInflate() { super.onFinishInflate(); // 文本框的text改变监听 mEditText.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public 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; } }
<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>运行实例:
喜欢的朋友欢迎关注