如何在Android中从2个radiobutton Kotlin中选择1个radiobutton
我想让用户只选择1个radiobutton,但每当我尝试更改choosen按钮时,他们不会切换到其他按钮,而是最终选择了2个radiobutton 结果总是这样,如果我按下其中一个按钮,它们不会互相切换: 更新如何在Android中从2个radiobutton Kotlin中选择1个radiobutton,android,xml,android-studio,kotlin,radio-button,Android,Xml,Android Studio,Kotlin,Radio Button,我想让用户只选择1个radiobutton,但每当我尝试更改choosen按钮时,他们不会切换到其他按钮,而是最终选择了2个radiobutton 结果总是这样,如果我按下其中一个按钮,它们不会互相切换: 更新 private fun pendaftaran( ) { val namaForm = findViewById<EditText>(R.id.inputNama) val tglLahirForm = findViewById&l
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()
}
}
}
}
private fun pendaftaran(){
val namaForm=findviewbyd(R.id.inputNama)
val tglLahirForm=findViewById(R.id.inputGllaHir)
val getRadioButton=onRadioButtonClicked(?)//我应该在参数中抛出什么?
}
单击收音机按钮(视图:视图){
val laki=findViewById(R.id.radioButtonLakiDaftar)
val getLaki=laki.text
val perempuan=findViewById(R.id.radioButtonPerempuanDaftar)
val getPerempuan=perempuan.text
如果(视图为单选按钮){
val checked=view.isChecked
何时(view.id){
R.id.radioButtonLakiDaftar->
如果(选中){
getLaki.toString()
}
R.id.radioButtonPerempuanDaftar->
如果(选中){
getPerempuan.toString()
}
}
}
}
您需要将单选按钮像这样放在xml中
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton android:id="@+id/radio_pirates"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/pirates"
android:onClick="onRadioButtonClicked"/>
<RadioButton android:id="@+id/radio_ninjas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/ninjas"
android:onClick="onRadioButtonClicked"/>
</RadioGroup>
您可以阅读它您需要使用RadioGroup,否则每个单选按钮将被视为一个单独的视图(radioButton),并且可以单独控制 RadioGroup提供了在多个选项中选择一个选项的控制 XML示例:
<?xml version="1.0" encoding="utf-8"?>
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/radioGroup"
android:orientation="vertical">
<RadioButton android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Radio1"
android:onClick="onRadioButtonClicked"/>
<RadioButton android:id="@+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Radio2"
android:onClick="onRadioButtonClicked"/>
</RadioGroup>
您需要将两个单选按钮放在一个单选组下。如果这不起作用,请添加xml。@akhilnair你有我可以观看/阅读的关于这方面的参考资料吗?谢谢你不接受我的答案的任何原因,如果你让我知道我是否可以改进它,这将有助于我将来写出更好的答案。@dinkar_kumar我只是困惑如果我在RadioButtonClicked(view:view)上制作一个新的
乐趣,那么我如何用其他方法调用它?假设我有单独的fun寄存器
如何调用fun onRadioButtonClicked(视图:视图)
?当我从文档中阅读时,它声明“注意:RadioGroup是LinearLayout的一个子类,默认情况下具有垂直方向。”,我可以在ConstraintLayout中使用RadioGroup吗?是的,您可以参考
fun onRadioButtonClicked(view: View) {
if (view is RadioButton) {
val checked = view.isChecked
when (view.getId()) {
R.id.radio1 ->
if (checked) {
// Pirates are the best
}
R.id.radio2 ->
if (checked) {
// Radio 2 is selected
}
}
}
}