Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

1.7K
Vistas
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 4 years ago · Hanz Gallego
2 Respuestas
Responde la pregunta

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 4 years ago · Hanz Gallego Denunciar

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 4 years ago · Hanz Gallego Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda