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

本文實現的效果:java

文本框輸入爲空時顯示輸入的圖標;不爲空時顯示清空的圖標,此時點擊清空圖標能清空文本框內輸入文字。android

實現效果:緩存

核心代碼:app

package com.example.test;

import android.app.Activity;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.EditText;

public class SearchActivity extends Activity {

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

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_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.edt_search);
        mSearchView.addTextChangedListener(tbxSearch_TextChanged);
        mSearchView.setOnTouchListener(txtSearch_OnTouch);
        //設置默認圖標
        mSearchView.setCompoundDrawablesWithIntrinsicBounds(null,
                null, mIconSearchDefault, null);
    }

    /**
     * 動態搜索
     */
    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) {
            //查詢搜索
        }
    };
    /** 點擊清除圖標,清空文本框*/
    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() - 88
                        && !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;
        }
    };
}

    代碼說明:ide

      1. 爲輸入框綁定觸摸事件(模擬點擊事件捕捉)。經過監聽點擊區域判斷是否點擊清空圖片,rest

                        若是在該區域而且文本框不爲空,則清空文本框。
code

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

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

相關文章
相關標籤/搜索