Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

Android 教學:為 EditText 加上搜尋與一鍵清空圖示

編輯:Android 開發實例

要做的效果

一個常見的搜尋框互動模式是:輸入框為空時,右側顯示搜尋圖示;一旦有內容,圖示自動切換成「清除」樣式,點它就清空文字。整個過程不需要額外按鈕,也不額外佔用版面。

監聽文字變化,切換右側圖示

核心是利用 TextWatcher 監聽內容變化,並以 setCompoundDrawablesWithIntrinsicBounds() 動態替換右側 compound drawable。這裡額外用一個 isnull 旗標快取上一次的狀態,避免每次輸入都觸發一次不必要的圖示重設。

/**
 * 動態切換搜尋 / 清除圖示
 */
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) {
    }
};

攔截點擊事件,判定是否点在清除圖示上

compound drawable 本身不帶點擊事件,因此改用 OnTouchListener 攔下 ACTION_UP,再用 event.getX() 判斷落點是否落在右側圖示區域。若是,且輸入框內容非空,就清空文字。

這裡有個關鍵細節:清空之後要暫時關掉軟鍵盤再還原,否則部分機型會因為輸入法重新__': mSearchView.setInputType(InputType.TYPE_NULL) 先停用輸入法,呼叫原生 onTouchEvent(event) 完成焦點處理,最後把原本的 inputType 設回去。

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(); // 備份輸入類型
                mSearchView.setInputType(InputType.TYPE_NULL);   // 暫時關閉軟鍵盤
                mSearchView.onTouchEvent(event);                 // 呼叫原生處理
                mSearchView.setInputType(cacheInputType);        // 還原輸入類型
                return true;                                     // 消費這次事件
            }
            break;
        }
        return false;
    }
};

值得留意的是 v.getWidth() - 38 這個門檻值。它對應圖示的實際寬度,實作時請依自己的圖示尺寸調整,不要照抄。

把監聽器綁到輸入框上

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. 點擊判定:透過 touch 事件模擬點擊,用 X 座標區間判斷是否命中清除圖示,命中且內容非空時清空輸入框。
  2. 圖示切換:透過 text 變更監聽,依內容有無動態設定 compound drawable。
  3. 鍵盤狀態:清空後保留原本的軟鍵盤狀態,避免使用者被迫重新點擊輸入框。

小結

這個範例沒有自訂 View,全靠 EditText 既有的 compound drawable、TextWatcher 與 OnTouchListener 三個機制組合而成。把控制項既有的屬性、方法與事件摸熟,很多看似需要客製元件才能做到的互動,其實用標準 API 就能完成。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved