我正在以编程方式创建带有两个单选按钮的单选组。最初,检查一个按钮。在创建时,当我单击另一个按钮时,最初选中的按钮的状态不会切换。
val medlayout: LinearLayout = findViewById(R.id.manuallayout)
var inputgroup = RadioGroup(this)
inputgroup.orientation = RadioGroup.HORIZONTAL
medlayout.addView(inputgroup)
var inputText = RadioButton(this)
inputText.text = "Type Name"
inputText.isChecked = true
inputgroup.addView(inputText)
var inputPic = RadioButton(this)
inputPic.text = "Take a Photo"
inputgroup.addView(inputPic)
我在这里想念什么
答案 0 :(得分:1)
您需要设置单选按钮的ID
val medlayout: LinearLayout = findViewById(R.id.manuallayout)
var inputgroup = RadioGroup(this)
inputgroup.orientation = RadioGroup.HORIZONTAL
medlayout.addView(inputgroup)
var inputText = RadioButton(this)
inputText.id = View.generateViewId() // Set ID for inputText
inputText.text = "Type Name"
inputText.isChecked = true
inputgroup.addView(inputText)
var inputPic = RadioButton(this)
inputPic.id = View.generateViewId() // Set ID for inputPic
inputPic.text = "Take a Photo"
inputgroup.addView(inputPic)
答案 1 :(得分:0)
只需将id设置为您的RadioButton,就像这样:
val medlayout: LinearLayout = findViewById(R.id.ll)
var inputgroup = RadioGroup(this)
inputgroup.orientation = RadioGroup.HORIZONTAL
medlayout.addView(inputgroup)
var inputText = RadioButton(this)
inputText.text = "Type Name"
inputText.id = 0
inputText.isChecked = true
inputgroup.addView(inputText)
var inputPic = RadioButton(this)
inputPic.text = "Take a Photo"
inputPic.id = 1
inputgroup.addView(inputPic)