Android 一次只选择一个单选按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10107532/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Only one radio button selected at a time
提问by user182192
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?
我在一个单选组中有两个单选按钮。我还有 2 个 androd:button checkbox- 用于取消选择单选按钮时和 checkbox_v 用于用户选择复选框时。我还实现了一种方法onRadioButtonClick
,以确保只有一个单选按钮具有 drawable:checkbox 而另一个具有 checkbox_v 。我怎样才能实现 onRadioClick 来做到这一点?任何的想法?
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
Drawbe- 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"/>
回答by Bill Gary
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.
如果它们在一个无线电组中,则一次只能选择一个。如果您希望两者都能被选中,请将它们从无线电组中删除。
回答by Bilal Haider
As the top voted answer didn't work for me as for many others. I am sharing the method that i used and its working perfectly.
由于投票最多的答案对我和其他许多人都不起作用。我正在分享我使用的方法及其完美的工作。
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checkedButton="@+id/rb_female"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rb_female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
android:button="@drawable/selector_radio_button"
android:text="Female" />
<RadioButton
android:id="@+id/rb_male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@drawable/selector_radio_button"
android:text="Male" />
</RadioGroup>
Setting the android:checkedButton="@+id/rb_female"
worked for me in RadioGroup
.
设置android:checkedButton="@+id/rb_female"
在为我工作RadioGroup
。
回答by timoschloesser
You should remove your onRadioButtonClick method and add OnCheckedChangeListener to your radiogroup. http://developer.android.com/reference/android/widget/RadioGroup.OnCheckedChangeListener.html
您应该删除 onRadioButtonClick 方法并将 OnCheckedChangeListener 添加到您的单选组。http://developer.android.com/reference/android/widget/RadioGroup.OnCheckedChangeListener.html
I guess by implementing your own click handler your are overwriting some functionality of the radiogroup.
我想通过实现您自己的点击处理程序,您正在覆盖无线电组的某些功能。
回答by Ajay Kumar Meher
Your requirement is not clear. Would you mind to make it simpler. I tried my best to understand your requirements and these are the findings given below :(.
你的要求不是很清楚。你介意让它更简单。我尽力了解您的要求,这些是下面给出的发现:(。
A radio button can have text. So you don't have to add another TextView
in your layout. Please remove them and add a simple android:text="blah.. blah.."
to your radiobutton.
单选按钮可以包含文本。所以你不必TextView
在你的布局中添加另一个。请删除它们并android:text="blah.. blah.."
为您的单选按钮添加一个简单的。
With my understanding you have two doubts.
根据我的理解,你有两个疑问。
How to customize RadioButtons with custom drawables??
如何使用自定义可绘制对象自定义 RadioButtons?
Checkout the answer here.
在此处查看答案。
How to deselect the previously selected radiobutton??
如何取消选择以前选择的单选按钮??
The truth is you don't have to do it manually as you are keeping those radiobuttons into a single radiogroup.
事实是您不必手动执行此操作,因为您将这些单选按钮保留在一个单选组中。
Hope this will help you.
希望这会帮助你。
回答by Anth
To check/uncheck radio buttons not part of a RadioGroup, add a method in the android GUI designer under the RadioButton Properties 'onClick' (In this example radio1_onClick).
要选中/取消选中不属于 RadioGroup 的单选按钮,请在 android GUI 设计器中的 RadioButton Properties 'onClick' 下添加一个方法(在本例中为 radio1_onClick)。
In your activity.xml under the xml for the RadioButton
在您的 activity.xml 下的 RadioButton xml
RadioButton
[blah blah blah]
android:id="@+id/radio1"
android:onClick="radio1_onClick"
In your activity_main.xml you would write the following method.
在您的 activity_main.xml 中,您将编写以下方法。
private void radio1_onClick(View v)
{
CompoundButton b=(CompoundButton)findViewById(R.id.radio1);
b.setChecked(true); /* light the button */
//b.setChecked(false); /* unlight the button */
}