我创建了一个包含2个线性布局的单选组。这2个线性布局分别包含1个单选按钮。创建线性布局仅是为了将样式应用于 radio 组和文本 View 。当我们选中单选按钮时,该按钮已经选中。它的工作正常,但我也想在单击线性布局时触发单选按钮。是否有解决此问题的解决方案。
`

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

请您参考如下方法:

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


评论关闭
IT序号网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!