Android 如何强制RadioGroup以编程方式一次只选择一个单选按钮?
我正在以编程方式设计自定义表单,用户可以在其中提出问题,并可以使用单选按钮添加多个选项。我参加了RadioGroup,并在其中添加了单选按钮。但在选择时,我希望一次只选择一个单选按钮。如何以编程方式实现它。请帮帮我 这是我的密码Android 如何强制RadioGroup以编程方式一次只选择一个单选按钮?,android,radio-button,android-radiogroup,Android,Radio Button,Android Radiogroup,我正在以编程方式设计自定义表单,用户可以在其中提出问题,并可以使用单选按钮添加多个选项。我参加了RadioGroup,并在其中添加了单选按钮。但在选择时,我希望一次只选择一个单选按钮。如何以编程方式实现它。请帮帮我 这是我的密码 final RadioGroup radioGroup = new RadioGroup(getApplicationContext()); radioGroup.setId(1); LinearLayout.LayoutParams p = new LinearLay
final RadioGroup radioGroup = new RadioGroup(getApplicationContext());
radioGroup.setId(1);
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
RadioButton radioButtonView = new RadioButton(getApplicationContext());
radioButtonView.setId(i++);
radioButtonView.setText(addnew);
radioGroup.addView(radioButtonView, p);
loption.addView(radioGroup, p);
提前感谢,请查看此信息,它将为您服务: 首次获取单选按钮:
RadioButton radioBalls = (RadioButton) findViewById(R.id.radioGameInPlayBalls);
RadioButton radioTime = (RadioButton) findViewById(R.id.radioGameInPlayTime);
radioBalls.setOnCheckedChangeListener(this);
radioTime.setOnCheckedChangeListener(this);
现在在@overide方法中:
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean c) {
switch (buttonView.getId()) {
case R.id.radioGameInPlayBalls:
if (radioTime.isChecked() && c) {
radioBalls.setChecked(false);
return;
}
radioBalls.setEnabled(c);
break;
case R.id.radioGameInPlayTime:
if (radioBalls.isChecked() && c) {
radioTime.setChecked(false);
return;
}
radioTime.setEnabled(c);
break;
default:
break;
}
}
似乎您正在为每个
RadioButton
创建一个新的RadioGroup
您应该将每个新的
单选按钮
添加到相同的放射组
。然后,RadioGroup
将确保此时只能选择一个RadioButton
。您应该使用RadioGroup检查单选按钮,而不是直接使用单选按钮。下面,我从选项列表中填充单选组,获取要检查的选项的单选按钮id,并在填充单选组后检查相应的按钮。如果通过索引确定选中的选项,情况也是如此
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Log.d(TAG, "onCheckedChanged " + buttonView.getId());
mRadioGroup.clearCheck();
if (isChecked) {
mRadioGroup.check(buttonView.getId());
}
}
int checkedId = -1;
for(JsonElement option : options){
RadioButton radioButton = new RadioButton(getContext());
radioGroup.addView(radioButton);
if(checkedOptionId.equals(id)){
checkedId = radioButton.getId();
}
}
radioGroup.check(checkedId);
当我插入LinearLayout将单选按钮放入网格时,我也尝试过同样的问题 我就是这样解决的。 考虑到我们有更多的放射组,我们知道按下的单选按钮。因此,只需将侦听器从所有RadioGroup中删除,然后取消选择所有单选按钮,将OnCheckdChangeListener重置为RadioGroup 这是我的代码:
onCheckedChangeListener= new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
clearAllRadioButton();
((RadioButton) binding.getRoot().findViewById(i)).setChecked(true);
resetListenerRadioGroup();
}
};
onCheckedChangeListener2 = new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
clearAllRadioButton();
((RadioButton) binding.getRoot().findViewById(i)).setChecked(true);
resetListenerRadioGroup();
}
};
binding.rg.setOnCheckedChangeListener(onCheckedChangeListener);
binding.rg2.setOnCheckedChangeListener(onCheckedChangeListener2);
private void clearAllRadioButton() {
binding.rg2.setOnCheckedChangeListener(null);
binding.rg.setOnCheckedChangeListener(null);
binding.failed.setChecked(false);
binding.draft.setChecked(false);
binding.sent.setChecked(false);
binding.inbox.setChecked(false);
}
private void resetListenerRadioGroup(){
binding.rg2.setOnCheckedChangeListener(onCheckedChangeListener2);
binding.rg.setOnCheckedChangeListener(onCheckedChangeListener);
}
*不要使用radioButton的clearCheck(),因为它在重置侦听器时没有很好的响应。请提供一些代码,说明您已经尝试过的内容以及哪些内容不适用于您。您现在的实现中存在什么问题?如果您尝试过任何编程方式,是否为每个
单选按钮
创建新的放射组
?如果所有的单选按钮
都进入同一个放射组
,你应该得到你想要的行为?或者代码片段来自代码中的多个位置?radioButtonView.setId(i++);i
在哪里?您是否在for循环中使用了整个代码?没问题:)如果您能将我的答案标记为解决方案(如果它确实有效的话),那就太好了。这同样适用于您之前提出的所有问题。