[Android]文本框實現搜索和清空效果

前言android

  本文實現的效果:文本框輸入爲空時顯示輸入的圖標;不爲空時顯示清空的圖標,此時點擊清空圖標能清空文本框內輸入文字。
 緩存

 

聲明ide

  歡迎轉載,但請保留文章原始出處:)
 spa

  農民伯伯:http://over140.blog.51cto.com/rest

 

正文code

  1、實現效果
 blog

    

       

 

  2、實現代碼
 事件

    監聽輸入圖片

     /**
     * 動態搜索
     
*/
    
private  TextWatcher tbxSearch_TextChanged  =   new  TextWatcher() {

        
// 緩存上一次文本框內是否爲空
         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) {
        }

        
/**
         * 隨着文本框內容改變更態改變列表內容
         
*/
        @Override
        
public   void  onTextChanged(CharSequence s,  int  start,  int  before,
                
int  count) {
            
        }
    };

     觸摸事件get

     private  OnTouchListener txtSearch_OnTouch  =   new  OnTouchListener() {
        @Override
        
public   boolean  onTouch(View v, MotionEvent event) {
            
switch  (event.getAction()) {
            
case  MotionEvent.ACTION_UP:
                
int  curX  =  ( int ) event.getX();
                
if  (curX  >  v.getWidth()  -   38
                        
&&   ! TextUtils.isEmpty(mSearchView.getText())) {
                    mSearchView.setText(
"" );
                    
int  cacheInputType  =  mSearchView.getInputType(); //  backup  the input type
                    mSearchView.setInputType(InputType.TYPE_NULL); //  disable soft input
                    mSearchView.onTouchEvent(event); //  call native handler
                    mSearchView.setInputType(cacheInputType); //  restore input  type
                     return   true ; //  consume touch even
                }
                
break ;
            }
            
return   false ;
        }
    };

    綁定事件

     private  Drawable mIconSearchDefault;  //  搜索文本框默認圖標
     private  Drawable mIconSearchClear;  //  搜索文本框清除文本內容圖標

    @Override
    
protected   void  onCreate(Bundle savedInstanceState) {
        
super .onCreate(savedInstanceState);
        setContentView(R.layout.main)
        
        
final  Resources res  =  getResources();
        mIconSearchDefault 
=  res.getDrawable(R.drawable.txt_search_default);
        mIconSearchClear 
=  res.getDrawable(R.drawable.txt_search_clear);
        
        mSearchView 
=  (EditText) findViewById(R.id.txtSearch);
        mSearchView.addTextChangedListener(tbxSearch_TextChanged);
        mSearchView.setOnTouchListener(txtSearch_OnTouch);
    }

    代碼說明:

      1. 爲輸入框綁定觸摸事件(模擬點擊事件捕捉)。經過監聽點擊區域判斷是否點擊清空圖片,若是在該區域而且文本框不爲空,則清空文本框。
 

      2. 爲輸入框綁定文本改變事件監聽,根據內容改變更態設置圖標顯示。

      3. 維持清空操做後軟鍵盤狀態。

 

  3、參考

    1.  how to block virtual keyboard while clicking on edittext in android?

 

  4、小圖標下載

      

    (右鍵另存爲便可。)

 

結束
 

  活用好每個控件的屬性、方法和事件能實現不少有意思的效果。歡迎你們交流。  

相關文章
相關標籤/搜索