Skip to content

Commit 878cb33

Browse files
committed
fix: 修复焦点更改监听多次触发的问题
1 parent b84202b commit 878cb33

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

inputview/src/main/kotlin/com/xiaocydx/inputview/EditTextManager.kt

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,13 @@ internal class EditTextManager(
144144
}
145145

146146
/**
147-
* 当动画开始时,隐藏水滴状指示器,避免动画运行时不断跨进程通信,进而造成卡顿。
148-
* 在实际场景中,交互可能是先选中[EditText]的内容,再点击其它地方切换[Editor],
149-
* 或者调用[EditorAnimator.requestSimpleAnimation]请求运行动画。
147+
* 在实际场景中,交互可能是先选中[EditText]的文本内容,再点击其它地方切换[Editor],
148+
* 当动画开始时,隐藏左右水滴状指示器,避免动画运行时不断跨进程通信,进而造成卡顿。
150149
*/
151150
private inner class HideTextSelectHandleOnStart : ReplicableAnimationCallback {
152-
override fun onAnimationStart(state: AnimationState) = forEach {
153-
it.hideTextSelectHandle(keepFocus = state.isIme(state.current))
151+
override fun onAnimationStart(state: AnimationState) = forEach { handle ->
152+
handle.takeIf { it.hasTextSelectHandleLeftRight() }
153+
?.hideTextSelectHandle(keepFocus = state.isIme(state.current))
154154
}
155155
}
156156

@@ -179,7 +179,7 @@ internal class EditTextManager(
179179
if (ev.action == MotionEvent.ACTION_UP && editText.showSoftInputOnFocus) {
180180
// 点击EditText显示IME,手指抬起时隐藏水滴状指示器,
181181
// 注意:此时隐藏,能确保手指抬起后完全看不到指示器。
182-
if (!imeShown()) hideTextSelectHandle()
182+
if (!imeShown()) hideTextSelectHandle(keepFocus = true)
183183
}
184184
if (ev.action != MotionEvent.ACTION_DOWN) {
185185
// 若EditText有左右水滴状指示器,则表示文本被选中,此时不显示IME
@@ -191,11 +191,17 @@ internal class EditTextManager(
191191
* 由于`textSelectHandleXXX`是Android 10才有的属性,即[EditText]的水滴状指示器,
192192
* 因此通过`clearFocus`隐藏水滴状指示器,若[keepFocus]为`true`,则重新获得焦点。
193193
*/
194-
fun hideTextSelectHandle(keepFocus: Boolean = true) {
194+
fun hideTextSelectHandle(keepFocus: Boolean) {
195195
val editText = get() ?: return
196196
if (!editText.hasFocus()) return
197-
editText.clearFocus()
198-
if (keepFocus) editText.requestFocus()
197+
if (keepFocus) {
198+
editText.withoutFocusChange {
199+
editText.clearFocus()
200+
editText.requestFocus()
201+
}
202+
} else {
203+
editText.clearFocus()
204+
}
199205
}
200206

201207
/**
@@ -206,5 +212,12 @@ internal class EditTextManager(
206212
val editText = get() ?: return false
207213
return editText.selectionStart != editText.selectionEnd
208214
}
215+
216+
private inline fun EditText.withoutFocusChange(action: () -> Unit) {
217+
val listener = onFocusChangeListener
218+
onFocusChangeListener = null
219+
action()
220+
onFocusChangeListener = listener
221+
}
209222
}
210223
}

0 commit comments

Comments
 (0)