Hello fellow Kotlinauts. Can someone point me in t...
# anko
c
Hello fellow Kotlinauts. Can someone point me in the direction of an example of using Anko to drive a textWatcher where the text input is being referenced through the Kotlin extensions? I’ve been trying to find one on the web, but have yet to be successful. Thanks.
a
classified.dev: can you elaborate? I'm using textWatcher just in addTextChangedListener; can you show your case?
c
Thanks Antonio. I'll put up what I've got when I get home.
Copy code
val watcher = object : TextWatcher {
            override fun afterTextChanged(s: Editable) {
                btnEncode.isEnabled = txtMasterKey.text.toString() != ""
            }

            override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
                //Do something or nothing.
            }

            override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
                //Do something or nothing

            }
        }
        txtMasterKey.addTextChangedListener(watcher)
So this creates a watcher and adds it to the the text field. I’ve seen anko examples of similar but they’re designed around using anko for the layout and involve placing the code into the layout directly, which is nice, but in my case I have an existing XML layout and I wanted to know how to attached the more readable and less fussy anko style watcher to an existing item.
a
hmm, I don't think there's more concise way to write what you just wrote. You may find desired view with some shorter version of find/findViewById or kotlinx, if that is what you meant. Anyone else got an idea?
c
Thanks Antonio. The anko example I've seen does away with the two unused functions but seemingly only works inside anko layouts. Pity, but I'm 🆗 with it.
a
Sorry for late reply, but... Hmm, how come it works only inside anko layouts? Any error?
c
I’ve not looked at it in a while. I’ll need to go back and refresh my memory. I’ll post when I’ve had a look.
a
sure!