[Android] text box for search and clearing

Source: Internet
Author: User

Preface

Result of this article: when the input text box is empty, the input icon is displayed. If it is not empty, the empty icon is displayed. Click the clear icon to clear the input text in the text box.

 

Statement

You are welcome to repost, but please keep the original source of the article :)

Blog: http://www.cnblogs.com

Farmer's uncle: http://over140.cnblogs.com

 

Body

I. Implementation results

    

       

 

II. Implementation Code

Listener Input

/**
* Dynamic search
*/
Private TextWatcher tbxSearch_TextChanged =   New TextWatcher (){

// Whether the last cached text box is empty
Private   Boolean Isnull =   True ;

@ Override
Public   Void AfterTextChanged (Editable s ){
If (TextUtils. isEmpty (s )){
If ( ! Isnull ){
MSearchView. setCompoundDrawablesWithIntrinsicBounds (
Null ,
Null , MIconSearchDefault, Null );
Isnull
=   True ;
}
}
Else {
If (Isnull ){
MSearchView. setCompoundDrawablesWithIntrinsicBounds (
Null ,
Null , MIconSearchClear, Null );
Isnull
=   False ;
}
}
}

@ Override
Public   Void BeforeTextChanged (CharSequence s, Int Start, Int Count,
Int After ){
}

/**
* The list content is dynamically changed as the text box Content Changes
*/
@ Override
Public   Void OnTextChanged (CharSequence s, Int Start, Int Before,
Int Count ){

}
};

Touch event

Private OnTouchListener txtSearch_OnTouch =   New OnTouchListener (){
@ Override
Public   Boolean <

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.