解決 Adapter 多個 Item 存在 EditText 監聽輸入問題

DevUtils Github

方法 註釋
bindListener 綁定事件
package dev.assist;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;

/**
 * detail: 解決 Adapter 多個 Item 存在 EditText 監聽輸入問題
 * @author Ttt
 */
public class EditTextWatcherAssist<T> {

    // ================
    // = 對外公開方法 =
    // ================

    /**
     * 綁定事件
     * @param text     待設置文本
     * @param position 索引
     * @param editText EditText
     * @param listener 輸入監聽回調事件
     */
    public void bindListener(final CharSequence text, final int position,
                             final EditText editText, final InputListener<T> listener) {
        bindListener(text, position, editText, null, listener);
    }

    /**
     * 綁定事件
     * @param text     待設置文本
     * @param position 索引
     * @param editText EditText
     * @param object   Object
     * @param listener 輸入監聽回調事件
     */
    public void bindListener(final CharSequence text, final int position,
                             final EditText editText, final T object, final InputListener<T> listener) {
        if (editText != null) {
            // 設置內容
            editText.setText(TextUtils.isEmpty(text) ? "" : text);
            // 清空焦點
            editText.clearFocus();
            // 設置獲取焦點事件
            editText.setOnFocusChangeListener(new FocusListener(position, editText, object, listener));
        }
    }

    // ================
    // = 內部判斷方法 =
    // ================

    /**
     * detail: 輸入監聽回調事件
     * @param <T> 泛型
     * @author Ttt
     */
    public interface InputListener<T> {

        /**
         * 文本改變監聽
         * @param charSequence 改變文本
         * @param editText     EditText
         * @param position     索引
         * @param object       Object
         */
        void onTextChanged(CharSequence charSequence, EditText editText, int position, T object);
    }

    // ================================
    // = 處理 Adapter Item - EditText =
    // ================================

    // Text 改變事件
    private TextWatcher mTextWatcher;
    // 獲得焦點的 EditText
    private EditText mFocusEdit;
    // 獲得焦點的索引
    private int mFocusPos;

    /**
     * 焦點改變
     * @param editText EditText
     * @param position 索引
     */
    private void focusChange(final EditText editText, final int position) {
        if (mTextWatcher != null) {
            if (mFocusEdit != null) {
                mFocusEdit.removeTextChangedListener(mTextWatcher);
            }
            mTextWatcher = null;
        }
        // 保存獲得焦點的 EditText
        mFocusEdit = editText;
        // 保存索引
        mFocusPos = position;
    }

    // =

    /**
     * detail: 焦點事件監聽
     * @author Ttt
     */
    private class FocusListener implements View.OnFocusChangeListener {

        // 當前索引
        private int position;
        // EditText
        private EditText editText;
        // Object
        private T object;
        // 輸入監聽事件
        private InputListener<T> listener;

        /**
         * 構造函數
         * @param position 索引
         * @param editText EditText
         * @param object   Object
         * @param listener 輸入監聽回調事件
         */
        public FocusListener(int position, EditText editText, T object, InputListener<T> listener) {
            this.position = position;
            this.editText = editText;
            this.object = object;
            this.listener = listener;
        }

        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus) {
                // 獲得焦點設置 View 操作
                focusChange(editText, position);
                // 判斷是否爲 null
                if (mTextWatcher == null) {
                    mTextWatcher = new TextWatcher() {
                        @Override
                        public void onTextChanged(CharSequence charSequence, int start, int before, int count) {
                            if (mFocusPos == position) {
                                if (listener != null) { // 觸發回調
                                    listener.onTextChanged(charSequence, editText, position, object);
                                }
                            }
                        }

                        @Override
                        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                        }

                        @Override
                        public void afterTextChanged(Editable s) {
                        }
                    };
                }
                if (mFocusEdit != null) { // 增加監聽
                    mFocusEdit.addTextChangedListener(mTextWatcher);
                }
            } else { // 失去焦點, 清空操作
                focusChange(null, -1);
            }
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章