一次仅选择一个单选按钮

编程入门 行业动态 更新时间:2024-10-25 02:19:56
本文介绍了一次仅选择一个单选按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个单选按钮组两个单选按钮。我也有2 androd:当单选按钮被取消选中,checkbox_v时,他的用户选中该复选框为按钮复选框 - 。我也implemnted,为了一个方法 onRadioButtonClick 来确保只有一个单选按钮有绘制:复选框和其他已checkbox_v。我怎样才能implemnt onRadioClick做到这一点?任何想法?

公共无效onRadioButtonClick(视图V){    单选按钮=(单选)V;    布尔checkBox1Selected;    布尔checkBox2Selected = FALSE;    Toast.makeText(MainActivity.this,        button.getId()+被选择。+ R.id.wificheckBox,        Toast.LENGTH_SHORT).show();    如果(button.getId()== R.id.wificheckBox){        //复选框选择的切换状态        checkBox1Selected = radiowifiButton.isChecked();        //确保其他复选框未选择        如果(checkBox2Selected){            radiomobileButton.setChecked(假);            checkBox2Selected = FALSE;        }        否则如果(button.getId()== R.id.wifimobilecheckBox){            //复选框选择的切换状态            checkBox2Selected = radiomobileButton.isChecked();            //确保其他复选框未选择            如果(checkBox1Selected){                radiowifiButton.setChecked(假);                checkBox1Selected = FALSE;            }        }    }

主要XML

< RadioGroup中    机器人:ID =@ + ID / radioGroup中    机器人:layout_width =WRAP_CONTENT    机器人:layout_height =WRAP_CONTENT     机器人:layout_alignParentLeft =真    机器人:layout_below =@ + ID / ll_1    机器人:layout_marginLeft =20dp><的LinearLayout    机器人:ID =@ + ID / ll_2    机器人:layout_width =WRAP_CONTENT    机器人:layout_height =WRAP_CONTENT    机器人:layout_alignParentLeft =真    机器人:layout_below =@ + ID / ll_1    机器人:layout_marginLeft =20dp    机器人:方向=横向>    <单选        机器人:ID =@ + ID / wifimobilecheckBox        机器人:layout_width =WRAP_CONTENT        机器人:layout_height =WRAP_CONTENT        机器人:按钮=@绘制/ button_radio        安卓:检查=真        安卓的onClick =onRadioButtonClick/>    <的TextView        机器人:layout_width =WRAP_CONTENT        机器人:layout_height =WRAP_CONTENT        机器人:layout_alignTop =@ + ID / wificheckBox        机器人:layout_toRightOf =@ + ID / wificheckBox        机器人:paddingLeft =15dp        机器人:文字=WiFi或移动网络        机器人:文字颜色=#333333        机器人:TEXTSIZE =20dp/>< / LinearLayout中><的LinearLayout    机器人:ID =@ + ID / ll_3    机器人:layout_width =WRAP_CONTENT    机器人:layout_height =WRAP_CONTENT    机器人:layout_alignParentLeft =真    机器人:layout_below =@ + ID / ll_2    机器人:paddingTop =20dp    机器人:layout_marginLeft =20dp    机器人:方向=横向>    <单选        机器人:ID =@ + ID / wificheckBox        机器人:layout_width =WRAP_CONTENT        机器人:layout_height =WRAP_CONTENT        机器人:按钮=@绘制/ button_radio        安卓的onClick =onRadioButtonClick/>    <的TextView        机器人:layout_width =WRAP_CONTENT        机器人:layout_height =WRAP_CONTENT        机器人:layout_alignTop =@ + ID / wificheckBox        机器人:layout_toRightOf =@ + ID / wificheckBox        机器人:paddingLeft =15dp        机器人:文字=无线网络连接        机器人:文字颜色=#333333        机器人:TEXTSIZE =20dp/>< / LinearLayout中>< / RadioGroup中>

Drawabe- button_radio

<项目的android:state_checked =真正的机器人:STATE_ pressed =false的机器人:可绘制=@绘制/ checkbox_v/><项目android:state_checked=\"false\"android:state_$p$pssed=\"false\"'android:drawable=\"@drawable/checkbox\"/>

解决方案

如果他们是在一个RadioGroup中,只有一个可以在选定的时间。如果要同时能够进行选择,从RadioGroup中移除它们。

I have two radio button in a radio group. I also have 2 androd:button checkbox- for when the radio button is deselected and checkbox_v for when he user selects the checkbox. I also implemnted that a method onRadioButtonClick in order to make sure that only one radio button had drawable:checkbox and the other has checkbox_v . How can I implemnt onRadioClick to do this? any idea?

public void onRadioButtonClick(View v) { RadioButton button = (RadioButton) v; boolean checkBox1Selected; boolean checkBox2Selected = false; Toast.makeText(MainActivity.this, button.getId()+ " was chosen."+R.id.wificheckBox, Toast.LENGTH_SHORT).show(); if ( button.getId() ==R.id.wificheckBox) { // Toggle status of checkbox selection checkBox1Selected = radiowifiButton.isChecked(); // Ensure that other checkboxes are not selected if (checkBox2Selected) { radiomobileButton.setChecked(false); checkBox2Selected = false; } else if (button.getId() ==R.id.wifimobilecheckBox) { // Toggle status of checkbox selection checkBox2Selected = radiomobileButton.isChecked(); // Ensure that other checkboxes are not selected if (checkBox1Selected) { radiowifiButton.setChecked(false); checkBox1Selected = false; } } }

main xml

<RadioGroup android:id="@+id/radioGroup" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/ll_1" android:layout_marginLeft="20dp"> <LinearLayout android:id="@+id/ll_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/ll_1" android:layout_marginLeft="20dp" android:orientation="horizontal" > <RadioButton android:id="@+id/wifimobilecheckBox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:button="@drawable/button_radio" android:checked="true" android:onClick="onRadioButtonClick" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignTop="@+id/wificheckBox" android:layout_toRightOf="@+id/wificheckBox" android:paddingLeft="15dp" android:text="WiFi or mobile network" android:textColor="#333333" android:textSize="20dp" /> </LinearLayout> <LinearLayout android:id="@+id/ll_3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/ll_2" android:paddingTop="20dp" android:layout_marginLeft="20dp" android:orientation="horizontal" > <RadioButton android:id="@+id/wificheckBox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:button="@drawable/button_radio" android:onClick="onRadioButtonClick" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignTop="@+id/wificheckBox" android:layout_toRightOf="@+id/wificheckBox" android:paddingLeft="15dp" android:text="WiFi " android:textColor="#333333" android:textSize="20dp" /> </LinearLayout> </RadioGroup>

Drawabe- button_radio

<item android:state_checked="true"android:state_pressed="false" android:drawable="@drawable/checkbox_v"/><item android:state_checked="false"android:state_pressed="false"'android:drawable="@drawable/checkbox"/>

解决方案

If they are in a radiogroup, only one can be selected at a time. If you want both to be able to be selected, remove them from the radiogroup.

更多推荐

一次仅选择一个单选按钮

本文发布于:2023-11-28 22:43:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1644238.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单选   按钮

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!