-
Notifications
You must be signed in to change notification settings - Fork 311
/
Copy pathEditTextWatcherAssist.java
343 lines (315 loc) · 10.5 KB
/
EditTextWatcherAssist.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
package dev.assist;
import android.text.Editable;
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, null);
}
/**
* 绑定事件
* @param text 待设置文本
* @param position 索引
* @param editText EditText
* @param object Object
* @param listener 输入监听回调事件
* @param otherListener 其他事件触发扩展抽象类
*/
public void bindListener(
final CharSequence text,
final int position,
final EditText editText,
final T object,
final InputListener<T> listener,
final OtherListener<T> otherListener
) {
if (editText != null) {
// 设置内容
editText.setText(text);
// 清空焦点
editText.clearFocus();
// 设置获取焦点事件
editText.setOnFocusChangeListener(new FocusListener(
position, editText, object, listener, otherListener
));
}
}
// =============
// = 内部判断方法 =
// =============
// =================================
// = 处理 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 final int position;
// EditText
private final EditText editText;
// Object
private final T object;
// 输入监听事件
private final InputListener<T> listener;
// 其他事件触发扩展抽象类
private final OtherListener<T> otherListener;
/**
* 构造函数
* @param position 索引
* @param editText EditText
* @param object Object
* @param listener 输入监听回调事件
* @param otherListener 其他事件触发扩展抽象类
*/
public FocusListener(
int position,
EditText editText,
T object,
InputListener<T> listener,
OtherListener<T> otherListener
) {
this.position = position;
this.editText = editText;
this.object = object;
this.listener = listener;
this.otherListener = otherListener;
}
@Override
public void onFocusChange(
View v,
boolean hasFocus
) {
if (mFocusPos == position) {
if (otherListener != null) {
otherListener.onFocusChange(
true, hasFocus,
editText, position, object
);
}
}
if (hasFocus) {
// 获得焦点设置 View 操作
focusChange(editText, position);
// 判断是否为 null
if (mTextWatcher == null) {
mTextWatcher = new TextWatcher() {
@Override
public void onTextChanged(
CharSequence text,
int start,
int before,
int count
) {
if (mFocusPos == position) {
if (otherListener != null) {
otherListener.onTextChanged(
text, start, before, count,
editText, position, object
);
}
if (listener != null) { // 触发回调
listener.onTextChanged(text, editText, position, object);
}
}
}
@Override
public void beforeTextChanged(
CharSequence text,
int start,
int count,
int after
) {
if (mFocusPos == position) {
if (otherListener != null) {
otherListener.beforeTextChanged(
text, start, count, after,
editText, position, object
);
}
}
}
@Override
public void afterTextChanged(Editable text) {
if (mFocusPos == position) {
if (otherListener != null) {
otherListener.afterTextChanged(
text, editText, position, object
);
}
}
}
};
}
if (mFocusEdit != null) { // 增加监听
mFocusEdit.addTextChangedListener(mTextWatcher);
}
} else { // 失去焦点, 清空操作
focusChange(null, -1);
}
if (mFocusPos == position) {
if (otherListener != null) {
otherListener.onFocusChange(
false, hasFocus,
editText, position, object
);
}
}
}
}
// ==========
// = 事件相关 =
// ==========
/**
* detail: 输入监听回调事件
* @param <T> 泛型
* @author Ttt
*/
public interface InputListener<T> {
/**
* 文本改变监听
* @param text 改变文本
* @param editText EditText
* @param position 索引
* @param object Object
*/
void onTextChanged(
CharSequence text,
EditText editText,
int position,
T object
);
}
/**
* detail: 其他事件触发扩展抽象类
* @param <T> 泛型
* @author Ttt
*/
public static abstract class OtherListener<T> {
// =========================
// = OnFocusChangeListener =
// =========================
/**
* 焦点触发方法
* @param before {@code true} 进入方法先触发, {@code false} 逻辑处理后再次触发
* @param hasFocus 是否获取焦点
* @param editText EditText
* @param position 索引
* @param object Object
*/
public void onFocusChange(
boolean before,
boolean hasFocus,
EditText editText,
int position,
T object
) {
}
// ===============
// = TextWatcher =
// ===============
/**
* 在文本变化前调用
* @param text 修改之前的文字
* @param start 字符串中即将发生修改的位置
* @param count 字符串中即将被修改的文字的长度, 如果是新增的话则为 0
* @param after 被修改的文字修改之后的长度, 如果是删除的话则为 0
* @param editText EditText
* @param position 索引
* @param object Object
*/
public void beforeTextChanged(
CharSequence text,
int start,
int count,
int after,
EditText editText,
int position,
T object
) {
}
/**
* 在文本变化后调用
* @param text 改变后的字符串
* @param start 有变动的字符串的位置
* @param before 被改变的字符串长度, 如果是新增则为 0
* @param count 添加的字符串长度, 如果是删除则为 0
* @param editText EditText
* @param position 索引
* @param object Object
*/
public void onTextChanged(
CharSequence text,
int start,
int before,
int count,
EditText editText,
int position,
T object
) {
}
/**
* 在文本变化后调用
* @param text 修改后的文字
* @param editText EditText
* @param position 索引
* @param object Object
*/
public void afterTextChanged(
Editable text,
EditText editText,
int position,
T object
) {
}
}
}