动态添加的RadioButton不监听onCheckChangedListener()

时间:2019-02-11 06:45:35

标签: android android-radiogroup oncheckedchanged

我需要动态添加RadioGroups和RadioButtons并生成所需的视图,并在动态创建的RadioGroup上实现onCheckChangedListener()。问题是,当选中一个新添加的按钮时,当我单击其他按钮时并没有取消选中它。我已经检查过-Issue in radiogroup while adding radiobuttons dynamically,但无济于事。

我正在处理一个片段,并在其中动态添加视图。单选按钮不显示检查更改的行为。请帮我解决这个问题。

//optionsGroup(RadioGroup),optionButton(RadioButton) 

    optionsGroup = new RadioGroup(getActivity());
    optionsGroup.setId(radioGroupCount);
    optionsGroup.setPadding(20, 20, 20, 20);
    optionsGroup.setLayoutParams(layparams);
    radioGroupList.add(optionsGroup);
    ArrayList<Options> RadioButtonOptions = question.getQuestionOptions().getOptions();
    for (int i = 0; i < RadioButtonOptions.size(); i++) {
        optionButton = new RadioButton(getActivity());
        optionButton.setId(i);
        optionButton.setText(RadioButtonOptions.get(i).getOptionValue());
        optionsGroup.addView(optionButton);
        optionsGroup.clearCheck();
    }
    optionsGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
          for(int rb=0;rb<optionsGroup.getChildCount();rb++){
              RadioButton btn = (RadioButton) optionsGroup.getChildAt(rb);
              if(checkedId==btn.getId()){
                  radioGroupAnswerList.clear();
                  radioGroupAnswerList.add(btn.getText().toString());
                  return;
              }

          }
        }
    });
    holderlayout.addView(optionsGroup);

2 个答案:

答案 0 :(得分:0)

我认为问题是您正在分配视图,然后分配其侦听器。所以请尝试一下

for (int i = 0; i < RadioButtonOptions.size(); i++) {
        optionButton = new RadioButton(getActivity());
        optionButton.setId(i);
        optionButton.setText(RadioButtonOptions.get(i).getOptionValue());


    optionsGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
          for(int rb=0;rb<optionsGroup.getChildCount();rb++){
              RadioButton btn = (RadioButton) optionsGroup.getChildAt(rb);
              if(checkedId==btn.getId()){
                  radioGroupAnswerList.clear();
                  radioGroupAnswerList.add(btn.getText().toString());
                  return;
              }

          }
        }
    });
 optionsGroup.addView(optionButton);
}

答案 1 :(得分:0)

如果一个单选按钮单击正确,您需要取消选中其他单选按钮?并且所有这些都将动态创建单选按钮和单选组。然后您必须执行此操作。

//optionsGroup(RadioGroup),optionButton(RadioButton) 

optionsGroup = new RadioGroup(getActivity());
optionsGroup.setId(radioGroupCount);
optionsGroup.setPadding(20, 20, 20, 20);
optionsGroup.setLayoutParams(layparams);
radioGroupList.add(optionsGroup);
ArrayList<Options> RadioButtonOptions = question.getQuestionOptions().getOptions();
for (int i = 0; i < RadioButtonOptions.size(); i++) {
    optionButton = new RadioButton(getActivity());
    optionButton.setId(i);
    optionButton.setText(RadioButtonOptions.get(i).getOptionValue());
    optionsGroup.addView(optionButton);
    optionsGroup.clearCheck();
}
optionsGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
      for(int rb=0;rb<optionsGroup.getChildCount();rb++){
          RadioButton btn = (RadioButton) optionsGroup.getChildAt(rb);
          if(checkedId==btn.getId()){
              radioGroupAnswerList.clear();
              radioGroupAnswerList.add(btn.getText().toString());
              optionsGroup.clearCheck();
               if (isChecked) {
                 optionsGroup.check(btn.getId());
              }
              return;
          }

      }
    }
});
holderlayout.addView(optionsGroup);