• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

1.5K
Views
Android EditText Coroutine operador de rebote como RxJava

Necesito hacer EditText con funcionalidad de sugerencia automática y necesito escuchar su entrada. También necesito ignorar el cambio de EditText cuando se configura mediante programación.

Me pregunto si hay una solución para hacer que EditText de rebote con Coroutines sin usar demora en esta situación.

over 3 years ago · Hanz Gallego
2 answers
Answer question

0

Después de investigar un poco sobre Coroutines y Flow, se me ocurrió una solución para crear EditText personalizado que contiene la lógica de rebote en su interior y me permite adjuntar TextWatcher de rebote y eliminarlo cuando lo desee. Aquí está el código de mi solución.

 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?, p1: Int, p2: Int, p3: Int) {} override fun afterTextChanged(p0: Editable?) { offer(p0.toString()) } } addTextChangedListener(textWatcher) awaitClose { removeTextChangedListener(textWatcher) } } }

Cuando quiero activar Debounce TextWatcher, simplemente llamo

 // 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) }

Tuve un problema con el enfoque al implementar DebounceEditText dentro de xml, por lo que tuve que configurar InTouchMode seleccionable, seleccionable y enfocable en verdadero.

 android:focusable="true" android:focusableInTouchMode="true" android:clickable="true"

En caso de que quiera configurar la entrada en DebounceEditText sin activar, simplemente elimine TextWatcher llamando

 debounceEditText.removeOnDebounceTextWatcher()
over 3 years ago · Hanz Gallego Report

0

Convierta el evento de cambio de texto en un Flow

Usar kotlinx.coroutines 1.5.0

 @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?, start: Int, before: Int, count: Int) { trySend(s) } } addTextChangedListener(listener) awaitClose { removeTextChangedListener(listener) } }.onStart { emit(text) } }

Si usa androidx.core.widget.doOnTextChanged :

 @ExperimentalCoroutinesApi @CheckResult fun EditText.textChanges(): Flow<CharSequence?> { return callbackFlow { checkMainThread() val listener = doOnTextChanged { text, _, _, _ -> trySend(text) } awaitClose { removeTextChangedListener(listener) } }.onStart { emit(text) } }

Utilizar:

 editText.textChanges().debounce(300) .onEach { ... } .launchIn(lifecycleScope)

Y algo como esto:

 fun executeSearch(term: String): Flow<SearchResult> { ... } editText.textChanges() .filterNot { it.isNullOrBlank() } .debounce(300) .distinctUntilChanged() .flatMapLatest { executeSearch(it) } .onEach { updateUI(it) } .launchIn(lifecycleScope)

Código fuente: https://github.com/Kotlin-Android-Open-Source/MVI-Coroutines-Flow/blob/master/core-ui/src/main/java/com/hoc/flowmvi/core_ui/FlowBinding.kt #L116

over 3 years ago · Hanz Gallego Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error