Answer the question
In order to leave comments, you need to log in
What is the difference between creating a TextWatcher in Android on Kotlin?
What is the difference between the two ways to create a TextWatcher
1st way
val textChangedListener = object: TextWatcher{
override fun afterTextChanged(p0: Editable?) {
}
override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
}
override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
}
}
private object textChangedListener: TextWatcher{
override fun afterTextChanged(p0: Editable?) {
}
override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
}
override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
}
}
Answer the question
In order to leave comments, you need to log in
Correct first. The word object has different meanings. In the first case, you create an object of an anonymous class that implements the listener. In the second case, you create a singleton textChangedListener of type textChangedListener (i.e. the names of the class and its only instance are the same. By the way, this is why singleton objects are usually named with a capital letter.). In the second case, of course, you cannot call other methods, because object is not an inner class. Thus, it is little more than completely useless.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question