我尝试在Android Kotlin中为checked / unchecked(checkBox)事件实现OnCheckedChangeListener。但它不起作用。这是我的代码: -
checkBox?.setOnCheckedChangeListener(object : CompoundButton.OnCheckedChangeListener {
override fun onCheckedChanged(buttonView: CompoundButton, isChecked: Boolean) {
if (checkBox!!.isChecked) {
Toast.makeText(applicationContext, "checked ", Toast.LENGTH_LONG).show()
}
else{
Toast.makeText(applicationContext, "unCkecked", Toast.LENGTH_LONG).show()
}
}
})
Pic在这里是我想要的: - Click Here
答案 0 :(得分:3)
我添加了自己的代码,借助lambda函数可以实现。
checkBoxSample.setOnCheckedChangeListener({
buttonView, isChecked ->
if (isChecked){
// show toast , check box is checked
}else{
// show toast , check box is not checked
}
})