Simple tutorial for Android-70th gun (custom and practical control-Email verification EditText)

Source: Internet
Author: User

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:

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.