How to use TextWatcher - methods - ```override fun...
# compose
s
How to use TextWatcher - methods -
Copy code
override fun afterTextChanged(editable: Editable?)
in a TextField component in Compose?
d
So the simplest thing I can think of is do this.
Copy code
LaunchedEffect(textFieldText) {
    // do afterTextChanged
}
Where
textFieldText
is the variable you pass into TextField.
There are other ways depending on your use case, but I'd need more info on what you're doing.
s
Thanks Dominic - this is good - But LaunchedEffect() gets called for every character I type - So within this LaunchedEffect(text!!) - I need to do an operation after entire text has been entered. so want an equivalent of afterTextChanged() so only after entire text is entered I can get a handle
d
How do you know when the text has stopped changing though? You don't really know when the user is done typing.
s
and do my operation only once instead of multiple times as every character gets entered in the Textfield
hmmm.
In conventional case
Copy code
tField!!.addTextChangedListener(object : TextWatcher {
    override fun beforeTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {}

    override fun onTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {

    }

    override fun afterTextChanged(editable: Editable) {}
})
I think afterTextChanged get called once focus is removed - am I right?
d
Ohhhhh, focus removal, there's probably a modifier for that.
(Sorry, haven't done Android in a long time)
s
I understand that we don’t know when the user has stopped typing - I am trying to see if performance can be improved here instead of doing of going thru mutiple times. I will explore focus modifier .
Thanks - Launchedeffect was very helpful
d
There's also
snapshotFlow { }
which will allow you to optimise with flow operators.
305 Views