AndroidUI design tour-custom EditText (one more clear button)

Source: Internet
Author: User

What happened to csdn? The uploaded code is messy .........

Today, I want to customize an EditText file. The main effect is that when no character is entered, there is no clear button at the end. No nonsense.

I. Results:

 

Is this effect cool? Encapsulate it into a class and you don't need to write it every time. Directly when the control is used.

Ii. Code

 

(1) user_definedet.xml

 

 
     
      
   
  
 


 

(2) UserDefineEditText. java
Package com. example. userdefinedet; import android. content. context; import android. text. editable; import android. text. textWatcher; import android. util. attributeSet; import android. util. log; import android. view. view; import android. view. view. onClickListener; import android. widget. editText; import android. widget. imageView; import android. widget. linearLayout; public class UserDefineEditText extends LinearLayout implements TextWatcher, OnClickListener {private EditText mEdiText; private ImageView mImageView; public UserDefineEditText (Context context) {super (context); initView ();} public UserDefineEditText (Context context, AttributeSet attrs, int defStyle) {super (context, attrs, defStyle); initView ();} public UserDefineEditText (Context context, AttributeSet attrs) {super (context, attrs); initView () ;}// initialize the private void initView () {View view = View. inflate (getContext (), R. layout. user_definedet, null); mEdiText = (EditText) view. findViewById (R. id. user_define_et); mImageView = (ImageView) view. findViewById (R. id. user_define_iv); mImageView. setVisibility (View. GONE); mEdiText. addTextChangedListener (this); mImageView. setOnClickListener (this); addView (view) ;}@ Overridepublic void afterTextChanged (Editable s) {// TODO Auto-generated methoif (s. length ()> 0) {mImageView. setVisibility (View. VISIBLE);} else {mImageView. setVisibility (View. GONE) ;}@ Overridepublic void beforeTextChanged (CharSequence s, int start, int count, int after) {// TODO Auto-generated method stub} @ Overridepublic void onTextChanged (CharSequence s, int start, int before, int count) {// TODO Auto-generated method stub} @ Overridepublic void onClick (View v) {switch (v. getId () {case R. id. user_define_iv: mEdiText. setText (); break; default: break;} public String getText () {return mEdiText. getText (). toString ();}}

(3) Two methods to use this control:
(1) new UserDefineEditText (this );
This indicates "context"
(2) Introduce the following in the layout file:
For example:

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.