首页 > 解决方案 > 未选中单选按钮,不使用单选组的清除检查功能,因为单选组清除检查进入循环

问题描述

您能否确定我在代码中做错了什么,因为我是 android 的新手并且不胜感激,如果您需要更多信息,请告诉我。

实际上我正在创建动态无线电组,虽然在视图寻呼机上使用表格/滑动布局等片段

我为清除无线电组/单选按钮所做的一切尝试

我已经尝试过radioGroup.clearCheck();,但在这种情况下,我遇到了循环问题,所以我只是跳过了这个,并想取消选中我当前处于监听器中的广播组的所有按钮,但是在这样做时,我面临着非常不可预测的行为,就像有些时候我不是能够检查另一个按钮或可以检查两个按钮

注意我在尝试什么:只是我改变了一些东西,我想要什么,单选组中有一组按钮,当我点击一个按钮时,它被选中,但我点击了同一个单选组的另一个按钮,然后之前选中按钮没有被取消选中,甚至新按钮也被选中,所以我所做的是,当被选中时调用并且代码到达我的听众那里我正在检查按钮是否已经被选中然后 io 正在尝试取消选中相同,所以那里我可以学习动态创建大量广播组并处理这些的任何文档或其他东西。我相信因为我正在动态创建按钮,所以我正面临这个问题

  private void checkOnClickedRadioGroupCalled(ArrayList<LinearLayout> setRadioGroupValuesLinearLayoutList) {
    for (final LinearLayout linearLayoutGroup : setRadioGroupValuesLinearLayoutList) {
        String linearLayoutTag = linearLayoutGroup.getTag() + Constant._RADIOGROUP;
        System.out.println("Radio group tag name : - " + linearLayoutTag);
        RadioGroup radioGroup = (RadioGroup) v.findViewWithTag(linearLayoutTag);
        if (radioGroup == null) {
            radioGroup = (RadioGroup) getRadioInitialize(linearLayoutTag);
        }
        if (radioGroup != null) {

            radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    listOfRadioButtons = new ArrayList<RadioButton>();
                    DynamicFieldUpdate.getButtonsCountAndInit(group,listOfRadioButtons);
                    RadioButton radioButtonInSideClickListner = (RadioButton) group.findViewById(checkedId);
                    /*RadioButton mButton = (RadioButton) group.getChildAt(0);
                    RadioButton mButton1 = (RadioButton) group.getChildAt(1);*/
                        if(radioButtonInSideClickListner!=null && radioButtonInSideClickListner.isChecked()) {
                            /*mButton.setChecked(false);
                            mButton1.setChecked(false);*/
                            for(int i = 0;i<listOfRadioButtons.size();i++){
                            if(radioButtonInSideClickListner.getId() == getViewId(listOfRadioButtons.get(i))) {
                                radioButtonInSideClickListner.setText(listOfRadioButtons.get(i).getText());
                                radioButtonInSideClickListner.setChecked(true);
                            } else{
                                radioButtonInSideClickListner.setText(radioButtonInSideClickListner.getText());
                                radioButtonInSideClickListner.setChecked(false);

                            }
                        }
                    }
                    addRadioGroupValuesViewListInputValue.add(group);

                }

                });

        }
    }

}

这是我创建广播组的代码

    public static void setCustomRadioGroup(LinearLayout layout, Context context, ArrayList<String> setName, ArrayList<View> viewList) {
        int sizeOfList = setName.size();
        final RadioButton[] radioButtons = new RadioButton[setName.size()];
        RadioGroup radioGroup = new RadioGroup(context);
        System.out.println(layout.getTag()+"_RadioGroup"+"   this is tag from dynamic layout creation");
        //set this in constant afterweard while dry run
        radioGroup.setTag(layout.getTag()+"_RadioGroup");
//      /*  LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
//                LinearLayout.LayoutParams.MATCH_PARENT,
//                LinearLayout.LayoutParams.WRAP_CONTENT
//        );
//        layout.addView(radioGroup, params);*/

        //radioGroup.setTag();//create the RadioGroup
        if (setName.size() < 3) {
            radioGroup.setOrientation(RadioGroup.HORIZONTAL);
        } else {
            radioGroup.setOrientation(RadioGroup.VERTICAL);
        }
        //or RadioGroup.VERTICAL
        for (int i = 0; i < setName.size(); i++) {
            radioButtons[i] = new RadioButton(context);
            radioButtons[i].setText(setName.get(i));
            String id = layout.getTag()+setName.get(i);
            radioButtons[i].setId(id.hashCode());
            Log.v("Selected", "New radio item id in Dynamic fiels: " + id.hashCode());

            radioGroup.addView(radioButtons[i]);
        }
        layout.addView(radioGroup);
        viewList.add(radioGroup);

    }

标签: javaandroidandroid-fragmentsandroid-radiobuttonandroid-radiogroup

解决方案


推荐阅读