Android Anko Radio Group 有默认值吗?

Android Anko Radio Group with default value?

我正在用 Kotlin 和 Anko 编写一个 android 应用程序,我试图给单选按钮一个默认值,但是当我这样做时,该值永远不会被取消选择。如何设置点击其他地方时更改的默认值?

radioGroup {
    orientation = LinearLayout.HORIZONTAL
    bottomPadding = dip(8)

    val radBut = { value: Int ->
        radioButton {
            text = "$value"
            onClick { Store.playerCount = value }
            if (Store.playerCount == value) {
                isChecked = true
            }
            rightPadding = dip(8)
        }
    }

    (2..5).map { radBut(it) }
}

不要直接更改单选按钮的 isChecked 属性 而是通过将单选按钮 id 提供给单选按钮 check(int id) 方法让单选组处理它,请查看此 link 了解更多信息详情 https://developer.android.com/reference/android/widget/RadioGroup#check(int) 这是给你的代码

   verticalLayout {
        orientation = LinearLayout.VERTICAL

        radioGroup {
            orientation = LinearLayout.HORIZONTAL
            padding = dip(8)

            val button1 = radioButton {
                text = "Button 1"
            }

            radioButton {
                text = "Button 2"
            }

            radioButton {
                text = "Button 3"
            }

            // Checking button 1 as default
            check(button1.id)
        }
    }