Home > database >  How to use doOnTextChanged() without overriding textWatcher?
How to use doOnTextChanged() without overriding textWatcher?

Time:10-07

I am not familiar with hints of Kotlin or Java in android studio. Most of them confuse me as a beginner to Android. For example what doe it means:

doOnTextChanged(CharSequence, Int, Int, Int)

I know that in overriding method (s:CharSequence, start: Int, before: Int, count: Int) first one is a variable that stores current text of textView. But how to use it here without overriding? The second is start third is before and last is count in overriding method. But I just know the role of s: CharSequence and don't know how to use other three stuffs in overriding method.

Is there any BOOK that explains these elementary concepts very easy?

CodePudding user response:

Without Kotlin, you would have to create a class that implements TextWatcher and override the onTextChanged() function to be able to react to the text as it changes.

With Kotlin, Android Jetpack provides a convenience TextView extension function with the doOnTextChanged which allows you to simply react to the TextView's text changes with a lambda function.

Without the Kotlin extension function, you would react to text changes by doing this:

myTextView.addTextChangedListener(object: TextWatcher {
    override fun afterTextChanged(s: Editable) {} // do nothing
    override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {} // do nothing

    override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int)  {
        // This function is called each time text in the text view is changed
        // s is the current text. The other parameters tell you which characters are
        // changed since the last time the function was called.

        // Put code in here that you want to run each time there's a change.
    }
})

With the Kotlin extension function, it is more concise because you don't have to write out the extra functions you aren't using:

myTextView.doOnTextChanged { s, start, before, count ->
    // The code in this lambda function is called each time text in the text view is changed
}

The Kotlin documentation is not very useful to a complete beginner. It is written as if you are already familiar with at least one object-oriented language. The Java documentation is much more beginner-friendly. Although the syntax is different, the teaching about the basic object-oriented concepts is very helpful to someone new to Kotlin and object-oriented programming. You can start here. The concept being used here is Interfaces.

Java doesn't have extension functions. That is a Kotlin feature that allows functions to be written for classes without overriding anything. In this case, the Jetpack authors use this feature to provide more concise syntax for Kotlin users by using features that could not be implemented in Java 7 (the version of Java Android uses for its standard library of code). If Android had been written with Java 8 or later, TextWatcher could have default empty implementations for the two less-frequently used functions, which would have eliminated the need for this Kotlin extension function to provide concise syntax.

Note that if you look at the source code for this extension function, it is actually still overriding TextWatcher. It's just doing it on your behalf under the hood.

CodePudding user response:

Other ways to do.

use this dependency in gradle

implementation 'androidx.core:core-ktx:latest_version'

you can simply use the below code.

edit_text.addTextChangedListener { it: Editable? ->
  // Do your stuff here
}

or

edit_text.doOnTextChanged { text, start, count, after -> // Do stuff }

The explanation:

This method is called to notify you that, within s, the count characters beginning at start have just replaced old text that had length before. It is an error to attempt to make changes to s from this callback.

for your ref:

https://developer.android.com/reference/android/text/TextWatcher.html

  • Related