Simple tutorial for Android-70th gun (custom and practical control-Email verification EditText)
We have customized an EditText that can verify whether the user's entered email address complies with the specifications.
1. layout file:
2. Custom Controls:
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 Draw AbleEditText extends RelativeLayout {private Context mContext; private EditText mEditText; private ImageView mImage; public DrawableEditText (Context context, AttributeSet attrs) {super (context, attrs); this. mContext = context; init (); // set the text size int textSize = attrs. getAttributeResourceValue (null, textSize, 0); if (textSize! = 0) {mEditText. setTextSize (textSize);} // when the hint of edittext is set, int hint = attrs. getAttributeResourceValue (null, hint, 0); if (hint! = 0) {mEditText. setHint (hint);} // set the text color int textColor = attrs. getAttributeResourceValue (null, textColor, 0); if (textColor! = 0) {mEditText. setTextColor (textColor) ;}// initialize the layout and control 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);} // set private void setDrawable () {if (checkEmail (mEditText. getText (). toString () {mImag E. setImageResource (R. drawable. right); // mImage. setImageResource (nullImgRes);} else {// mImage. setImageResource (imgRes); mImage. setImageResource (R. drawable. close) ;}@ Overrideprotected void onFinishInflate () {super. onFinishInflate (); // The text field changes to listen to mEditText. addTextChangedListener (new TextWatcher () {@ Overridepublic void onTextChanged (CharSequence s, int start, int before, int count) {}@ Overridepublic vo Id beforeTextChanged (CharSequence s, int start, int count, int after) {}@ Overridepublic void afterTextChanged (Editable s) {setDrawable ();}});} /*** verify email ** @ 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. Introduce controls:
Running instance: