【问题标题】:Radio button will select when linear layout clicked in android当在android中单击线性布局时,单选按钮将选择
【发布时间】:2020-01-07 05:08:33
【问题描述】:

我创建了一个包含 2 个线性布局的 1 个单选组。这 2 个线性布局各包含 1 个单选按钮。线性布局仅用于为单选组和文本视图应用样式。当我们检查该按钮已检查的单选按钮时。它工作正常,但我也想在单击线性布局时触发单选按钮。有没有办法解决这个问题。 `

                       <RadioGroup

                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginTop="15dp"
                          >
                            <LinearLayout
                                android:id="@+id/layout1"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:orientation="vertical"
                                style="@style/RadioButton">
                                <RadioButton
                                    android:id="@+id/entireplace"
                                    android:layout_width="match_parent"
                                    android:layout_height="wrap_content"
                                    android:button="@null"                                       
                                    android:textColor="@android:color/tab_indicator_text"                                        
                                    android:drawableRight="@drawable/custom_checkbox"
                                    android:text="@string/entireplace"
                                    />
                                <TextView

                                    android:layout_width="match_parent"
                                    android:layout_height="wrap_content"                                       
                                    android:text="@string/entire_txt"/>
                                <View
                                    android:layout_width="match_parent"
                                    android:layout_height="0.3dp"/>
                            </LinearLayout>


                            <LinearLayout
                                android:id="@+id/layout2"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:orientation="vertical"
                                style="@style/RadioButton">
                                <RadioButton
                                    android:id="@+id/privatepalce
                                    android:layout_width="match_parent"
                                    android:layout_height="wrap_content"
                                    android:button="@null"
                                    android:drawablePadding="30dp"                                       
                                    android:drawableRight="@drawable/custom_checkbox"
                                    android:text="@string/privateroom"
                                    />
                                <TextView

                                    android:layout_width="match_parent"
                                    android:layout_height="wrap_content"
                                    android:text="@string/pvt_txt"/>
                                <View
                                    android:layout_width="match_parent"
                                    android:layout_height="0.3dp""/>
                            </LinearLayout>   

                        </RadioGroup>

`

  val onRadioButtonCheckedListener =
            CompoundButton.OnCheckedChangeListener { buttonView, isChecked ->
                if (!isChecked) return@OnCheckedChangeListener
                if (previousCheckedCompoundButton != null) {
                    previousCheckedCompoundButton!!.isChecked = false
                    previousCheckedCompoundButton = buttonView
                } else {
                    previousCheckedCompoundButton = buttonView
                }
            }


        binding.entireplace.setOnCheckedChangeListener(onRadioButtonCheckedListener)
        binding.private.setOnCheckedChangeListener(onRadioButtonCheckedListener)

【问题讨论】:

  • 您可以将 onClick 侦听器设置为 LinearLayout,然后在 on Click 方法中设置单选按钮

标签: android kotlin android-linearlayout android-radiobutton android-radiogroup


【解决方案1】:

不要在 radiogroup 中放置线性布局,否则单选按钮将充当单独的单选按钮(这意味着每个单选按钮都可以一次选择或取消选择)。 您要求在单击线性布局时选中/取消选中单选按钮的第二件事,可以通过在单击线性布局时调用单选按钮的 setChecked(boolean) 方法来完成。

【讨论】:

    【解决方案2】:

    您不能将任何布局作为子项放在 RadioGroup 中

    RadioGroup 只能将 RadioButton 作为子项处理

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多