Skip to content

Instantly share code, notes, and snippets.

@LouisCAD
Last active June 4, 2018 13:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LouisCAD/524f49d50e15e22af40be2aa33f8a31e to your computer and use it in GitHub Desktop.
Save LouisCAD/524f49d50e15e22af40be2aa33f8a31e to your computer and use it in GitHub Desktop.
EditText TextWatcher, without the override me again ceremony
import android.text.Editable
import android.widget.EditText
import splitties.collections.forEachByIndex
import android.text.TextWatcher as AndroidTextWatcher
interface TextWatcher : AndroidTextWatcher {
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) = Unit
}
private class AfterTextChangedWatcher(private val listener: (Editable) -> Unit) : TextWatcher {
override fun afterTextChanged(s: Editable) = listener(s)
}
fun List<EditText>.afterAnyTextChanged(listener: (Editable) -> Unit) {
val watcher = AfterTextChangedWatcher(listener)
forEachByIndex { it.addTextChangedListener(watcher) }
}
fun EditText.afterTextChanged(listener: (Editable) -> Unit) {
addTextChangedListener(AfterTextChangedWatcher(listener))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment