Created
August 1, 2019 10:28
-
-
Save EmmanuelGuther/4bda5a766aeb825f0de3fa3e005820a1 to your computer and use it in GitHub Desktop.
A kotlin extension
This file contains hidden or 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
fun TextView.afterTextChangedDelayed(afterTextChanged: (String) -> Unit) { | |
this.addTextChangedListener(object : TextWatcher { | |
var timer: CountDownTimer? = null | |
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(editable: Editable?) { | |
timer?.cancel() | |
timer = object : CountDownTimer(1000, 1500) { | |
override fun onTick(millisUntilFinished: Long) {} | |
override fun onFinish() { | |
afterTextChanged.invoke(editable.toString()) | |
} | |
}.start() | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment