java editview监听输入,解决 Adapter 多个 Item 存在 EditText 监听输入问题

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 {

// ================

// = 对外公开方法 =

// ================

/**

* 绑定事件

* @param text 待设置文本

* @param position 索引

* @param editText EditText

* @param listener 输入监听回调事件

*/

public void bindListener(final CharSequence text, final int position,

final EditText editText, final InputListener 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 listener) {

if (editText != null) {

// 设置内容

editText.setText(TextUtils.isEmpty(text) ? "" : text);

// 清空焦点

editText.clearFocus();

// 设置获取焦点事件

editText.setOnFocusChangeListener(new FocusListener(position, editText, object, listener));

}

}

// ================

// = 内部判断方法 =

// ================

/**

* detail: 输入监听回调事件

* @param 泛型

* @author Ttt

*/

public interface InputListener {

/**

* 文本改变监听

* @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 listener;

/**

* 构造函数

* @param position 索引

* @param editText EditText

* @param object Object

* @param listener 输入监听回调事件

*/

public FocusListener(int position, EditText editText, T object, InputListener 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);

}

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值