问题描述
我需要使EditText具有自动建议功能,并且需要听其输入。以编程方式设置EditText时,我还需要忽略它。
想知道是否有解决方案可以在不使用延迟的情况下使用协同程序对EditText进行去抖动。
解决方法
将文本更改事件转换为Flow
@ExperimentalCoroutinesApi
@CheckResult
fun EditText.textChanges(): Flow<CharSequence?> {
return callbackFlow<CharSequence?> {
val listener = object : TextWatcher {
override fun afterTextChanged(s: Editable?) = Unit
override fun beforeTextChanged(s: CharSequence?,start: Int,count: Int,after: Int) = Unit
override fun onTextChanged(s: CharSequence?,before: Int,count: Int) {
offer(s)
}
}
addTextChangedListener(listener)
awaitClose { removeTextChangedListener(listener) }
}.onStart { emit(text) }
}
并使用:
editText.textChanges().debounce(300)
.onEach { ... }
.launchIn(lifecycleScope)
是这样的:
fun executeSearch(term: String): Flow<SearchResult> { ... }
editText.textChanges()
.distinctUntilChanged()
.filterNot { it.isNullOrBlank() }
.debounce(300)
.flatMapLatest { executeSearch(it) }
.onEach { updateUI(it) }
.launchIn(lifecycleScope)
,
在对协程和流程进行了一些研究之后,我想到了创建自定义EditText的解决方案,该自定义EditText在其中包含了防反跳逻辑,并使我能够附加防反跳TextWatcher并在需要时将其删除。这是我的解决方案的代码
class DebounceEditText @JvmOverloads constructor(
context: Context,attributeSet: AttributeSet? = null,defStyleAttr: Int = 0
) : AppCompatEditText(context,attributeSet,defStyleAttr) {
private val debouncePeriod = 600L
private var searchJob: Job? = null
@FlowPreview
@ExperimentalCoroutinesApi
fun setOnDebounceTextWatcher(lifecycle: Lifecycle,onDebounceAction: (String) -> Unit) {
searchJob?.cancel()
searchJob = onDebounceTextChanged()
.debounce(debouncePeriod)
.onEach { onDebounceAction(it) }
.launchIn(lifecycle.coroutineScope)
}
fun removeOnDebounceTextWatcher() {
searchJob?.cancel()
}
@ExperimentalCoroutinesApi
private fun onDebounceTextChanged(): Flow<String> = channelFlow {
val textWatcher = object : TextWatcher {
override fun beforeTextChanged(p0: CharSequence?,p1: Int,p2: Int,p3: Int) {}
override fun onTextChanged(p0: CharSequence?,p3: Int) {}
override fun afterTextChanged(p0: Editable?) {
offer(p0.toString())
}
}
addTextChangedListener(textWatcher)
awaitClose {
removeTextChangedListener(textWatcher)
}
}
}
当我想激活Debounce TextWatcher时,我只是打电话
// lifecycle is passed from Activity/Fragment lifecycle,because we want to relate Coroutine lifecycle with the one DebounceEditText belongs
debounceEditText.setOnDebounceTextWatcher(lifecycle) { input ->
Log.d("DebounceEditText",input)
}
在xml中实现DebounceEditText时,焦点出现问题,因此我必须将clickable,selectable和focusableInTouchMode设置为true。
android:focusable="true"
android:focusableInTouchMode="true"
android:clickable="true"
如果我想在DebounceEditText中设置输入而无需触发,只需通过调用删除TextWatcher
debounceEditText.removeOnDebounceTextWatcher()