Last active
June 4, 2018 13:00
-
-
Save LouisCAD/524f49d50e15e22af40be2aa33f8a31e to your computer and use it in GitHub Desktop.
EditText TextWatcher, without the override me again ceremony
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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