我正在用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) }
}
答案 0 :(得分:2)
不要直接更改单选按钮的isChecked属性,而是让单选按钮组通过将单选按钮ID赋予单选按钮组check(int id)方法来处理它,请检查此链接以获取更多详细信息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)
}
}