Created
October 7, 2021 13:16
-
-
Save Sardorbekcyber/573e7d6f7ac3e007f1cb60e43936bade to your computer and use it in GitHub Desktop.
This example demonstrates how to override TextWatcher's only after text changed method
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 TextInputEditText.textChangedDebounce(debounceTime: Long = 100L, action: (text: String) -> Unit) { | |
this.addTextChangedListener(object : TextWatcher { | |
private var lastTextChangedTime: Long = 0 | |
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {} | |
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {} | |
override fun afterTextChanged(s: Editable?) { | |
if (SystemClock.elapsedRealtime() - lastTextChangedTime < debounceTime) return | |
else action(s.toString()) | |
lastTextChangedTime = SystemClock.elapsedRealtime() | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment