首页 > 解决方案 > radioGroup.setOnCheckedChangeListener 给出不兼容的类型:RadioButton 和 Int

问题描述

我正在学习有趣的 Kotlin、Android Studio 和 viewBinding。这对我来说是麻烦的开始。我怎样才能使这个代码工作?

binding.radioGroup.setOnCheckedChangeListener { group, checkedId ->
    val rbtn: RadioButton = radioGroup.findViewById<RadioButton>(checkedId) 

    when (rbtn.id) {
        binding.radioButton1 -> 
            Toast (...)
        binding.radioButton2 -> 
            Toast (...)
        binding.radioButton3 -> 
            Toast (...)
        binding.radioButton4 -> 
            Toast (...)
    }
}

binding.radioButton1(或任何其他)给出不兼容的类型:RadioButton 和 Int 错误。

标签: android-radiogroup

解决方案


推荐阅读