Android 设置默认选中的单选按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20610492/
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
Set default checked Radio Button
提问by thehindutimes
I'm creating a RadioGroup
with RadioButton
s dynamically and need to have one radio button checked by default.
我正在动态创建一个RadioGroup
with RadioButton
s 并且需要默认选中一个单选按钮。
I've done this by using both radioButton.setChecked(true)
and radioButton.toggle();
我已经通过使用radioButton.setChecked(true)
和radioButton.toggle();
The problem I have is that when I at runtime select another radio button the first one stays checked so I end up with two checked radio buttons in the radio group.
我遇到的问题是,当我在运行时选择另一个单选按钮时,第一个单选按钮保持选中状态,因此我最终在单选组中得到两个选中的单选按钮。
Has anyone had this problem and know how to solve it?
有没有人遇到过这个问题并知道如何解决?
private RadioButton addRadioButton(String type, String price){
RadioButton radio = new RadioButton(Order.this);
radio.setText(type + " (" + Utils.formatCurrency(price) + ")");
radio.setTextAppearance(Order.this, R.style.portalCellTextStyle);
radio.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10);
radio.setTag(price);
if(type.toLowerCase().equals("m"))
radio.toggle();
return radio;
}
回答by Sergio Carvalho
I know I'm late but for those who search for the answer like me this can be useful.
我知道我迟到了,但对于像我这样寻找答案的人来说,这可能很有用。
When you add RadioButton to a RadioGroup, you must set the button checked after adding it to the parent like:
将 RadioButton 添加到 RadioGroup 时,必须在将其添加到父级后设置选中的按钮,例如:
RadioGroup radioGroup = new RadioGroup(getContext())
RadioButton radioButton = new RadioButton(getContext());
radioButton.setText("text");
mRadioGroup.addView(radioButton);
radioButton.setChecked(true);
If the view is checked before adding it to the parent, it will be impossible to uncheck it.
如果视图在添加到父视图之前被选中,则无法取消选中它。
回答by dorgelesn
You can do this simply:
你可以简单地做到这一点:
JAVA:
爪哇:
radiogroup =(RadioGroup)findViewById(R.id.radiogroup);
radiobutton1 =(RadioButton)findViewById(R.id.radiobutton1);
radiobutton2 =(RadioButton)findViewById(R.id.radiobutton2);
if(your first assessment){
radiobutton1.setChecked(true);
}else{
radiobutton2.setChecked(true);
}
XML:
XML:
<RadioGroup
android:id="@+id/radiogroup"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/radiobutton1"
android:checked="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:id="@+id/radiobutton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RadioGroup>
回答by Maulik Santoki
Simple, at first time load the screen check the position of In Getview
method and then apply condition:
简单,在第一次加载屏幕时检查 InGetview
方法的位置,然后应用条件:
if (position == 0) {
holder.act_delivery_rbtn.setChecked(true);
}
回答by Dostonbek Oripjonov
Just make checked = "true" in your code
只需在您的代码中进行检查 = "true"
回答by SilentKiller
If you are only using one radio box for checking on and off, maybe you should use checkbox or toggle button instead.
如果您只使用一个单选框来打开和关闭,也许您应该使用复选框或切换按钮。
http://developer.android.com/resources/tutorials/views/hello-formstuff.html
http://developer.android.com/resources/tutorials/views/hello-formstuff.html
Scroll down and see checkbox and toggle button.
向下滚动并查看复选框和切换按钮。
When using radios you usually have more than one and choose between them. Like easy, medium, hard.
使用收音机时,您通常有多个收音机并在它们之间进行选择。喜欢简单,中等,困难。
Replace
代替
radio.toggle();
with
和
radio.setChecked(true);
回答by nitesh goel
you should check it in radio group...
你应该在广播组里查一下...
radiogroup.check(IdOfYourButton)
回答by Yogamurthy
use the clearCheck () function to clear the already checked buttons.I hope this solves your problem.
使用 clearCheck () 函数清除已选中的按钮。希望这能解决您的问题。
回答by thehindutimes
Turned out to be because I called the checked() method on the radio group straight after setting the OnCheckedChangeListener() causing it to be called immediately and throw a NPE for some reason.
原来是因为我在设置 OnCheckedChangeListener() 后直接在无线电组上调用了 checked() 方法,导致它立即被调用并由于某种原因抛出 NPE。
Moved the checked() method call to just before the OnCheckedChangeListener() and it works now.
将 checked() 方法调用移动到 OnCheckedChangeListener() 之前,现在可以使用了。
radios.check(2);
radios.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
}
});
private RadioButton addRadioButton(String type, String price){
RadioButton radio = new RadioButton(Order.this);
radio.setText(type + " (" + Utils.formatCurrency(price) + ")");
radio.setTextAppearance(Order.this, R.style.portalCellTextStyle);
radio.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10);
radio.setTag(price);
if(type.toLowerCase().equals("m"))
radio.setId(2);
return radio;
}