Skip to content

Instantly share code, notes, and snippets.

@hector6872
Created January 16, 2018 10:57
Show Gist options
  • Save hector6872/58d64be39128edf41527ed79a75e7108 to your computer and use it in GitHub Desktop.
Save hector6872/58d64be39128edf41527ed79a75e7108 to your computer and use it in GitHub Desktop.
EditTextExtensions Kotlin
inline fun TextView.textWatcher(init: CustomTextWatcher.() -> Unit) = addTextChangedListener(CustomTextWatcher().apply(init))
@Suppress("unused")
class CustomTextWatcher : TextWatcher {
private var _beforeTextChanged: ((CharSequence?, Int, Int, Int) -> Unit)? = null
private var _onTextChanged: ((CharSequence?, Int, Int, Int) -> Unit)? = null
private var _afterTextChanged: ((Editable?) -> Unit)? = null
private var _beforeTextChangedShout: (() -> Unit)? = null
private var _onTextChangedShout: (() -> Unit)? = null
private var _afterTextChangedShout: (() -> Unit)? = null
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
_beforeTextChanged?.invoke(s, start, count, after)
_beforeTextChangedShout?.invoke()
}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
_onTextChanged?.invoke(s, start, before, count)
_onTextChangedShout?.invoke()
}
override fun afterTextChanged(s: Editable?) {
_afterTextChanged?.invoke(s)
_onTextChangedShout?.invoke()
}
fun beforeTextChanged(listener: (CharSequence?, Int, Int, Int) -> Unit) {
_beforeTextChanged = listener
}
fun onTextChanged(listener: (CharSequence?, Int, Int, Int) -> Unit) {
_onTextChanged = listener
}
fun afterTextChanged(listener: (Editable?) -> Unit) {
_afterTextChanged = listener
}
fun beforeTextChangedShout(listener: () -> Unit) {
_beforeTextChangedShout = listener
}
fun onTextChangedShout(listener: () -> Unit) {
_onTextChangedShout = listener
}
fun afterTextChangedShout(listener: () -> Unit) {
_afterTextChangedShout = listener
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment