Kotlin Android - How to implement CheckBox.OnCheckedChangeListener?
I am new to Kotlin. I created a snippet and implemented View.OnClickListener
and CheckBox.OnCheckedChangeListener
. View.OnClickListener
works as expected but shows an Unresloved reference for CheckBox.OnCheckedChangeListener
.
Code below
class LoginFragment : Fragment(), View.OnClickListener, CheckBox.OnCheckedChangeListener {
override fun onClick(view: View?) {
}
}
How can I implement CheckBox.OnCheckedChangeListener
..? thanks in advance
source to share
CheckBox.OnClickListener
is not an existing interface. CheckBox
inherits from View
and therefore CheckBox
you can use a method setOnClickListener
that takes an instance to assign a listener View.OnClickListener
.
If you want to handle both of these events in the same way Fragment
, you have to distinguish between the CheckBox
others View
using a method parameter onClick
.
Alternatively, you can use lambdas as listeners for yours View
instead of itself Fragment
.
checkbox.setOnClickListener { view ->
// handle clicks here
}
Usage setOnCheckedChangeListener
as stated in other answers is also an option with CheckBox
.
source to share