【问题标题】:how to chose 1 radiobutton from 2 RadioButton Kotlin Android如何从 2 RadioButton Kotlin Android 中选择 1 个单选按钮
【发布时间】: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


【解决方案1】:

你需要使用 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>

onRadioButtonClicked看到在两个单选按钮中我们调用了相同的方法,所以我们可以轻松控制它

    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
                }
        }
    }
}

【讨论】:

    【解决方案2】:

    您需要将这样的单选按钮放在 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>
    

    你可以阅读它here

    【讨论】:

    • 我从文档中读到,它说“注意:RadioGroup 是 LinearLayout 的子类,默认情况下具有垂直方向。”,我可以在 ConstraintLayout 中使用 RadioGroup 吗?
    猜你喜欢
    • 2014-03-07
    • 2022-09-22
    • 1970-01-01
    • 2012-07-21
    • 1970-01-01
    • 1970-01-01
    • 2021-11-08
    • 2010-10-23
    • 2020-06-11
    相关资源
    最近更新 更多