[在android中单击线性布局时将选择单选按钮

问题描述 投票:1回答:1

我创建了一个包含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)
android kotlin android-linearlayout android-radiobutton android-radiogroup
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.