【发布时间】:2021-07-29 19:10:10
【问题描述】:
我想让用户只选择 1 个单选按钮,但是每当我尝试更改选择的按钮时,他们不会相互切换,而是最终选择了 2 个单选按钮。
结果总是这样,如果我按其中一个,它们就不会相互切换:
更新
private fun pendaftaran( ) {
val namaForm = findViewById<EditText>(R.id.inputNama)
val tglLahirForm = findViewById<EditText>(R.id.inputTglLahir)
val getRadioButton = onRadioButtonClicked(?) // what should i throw inside the params?
}
fun onRadioButtonClicked(view: View) {
val laki = findViewById<RadioButton>(R.id.radioButtonLakiDaftar)
val getLaki = laki.text
val perempuan = findViewById<RadioButton>(R.id.radioButtonPerempuanDaftar)
val getPerempuan = perempuan.text
if(view is RadioButton) {
val checked = view.isChecked
when(view.id) {
R.id.radioButtonLakiDaftar ->
if(checked) {
getLaki.toString()
}
R.id.radioButtonPerempuanDaftar ->
if(checked) {
getPerempuan.toString()
}
}
}
}
【问题讨论】:
-
您需要将两个单选按钮放在一个单选组下。如果这不起作用,请添加 xml。
-
@akhilnair 你有我可以观看/阅读的参考资料吗?谢谢
-
任何你不接受我的答案的原因,如果你让我知道我是否可以改进它,它将帮助我将来写出更好的答案。
-
@dinkar_kumar 如果我创建一个新的
fun onRadioButtonClicked(view: View)我只是感到困惑,那么我怎么能用其他方法调用它呢?假设我有单独的fun register怎么叫fun onRadioButtonClicked(view: View)到那里?
标签: android xml android-studio kotlin radio-button