未选中单选按钮,而未使用单选按钮组的清除检查功能,因为单选按钮组清除检查会循环进行

时间:2018-07-23 13:05:51

标签: java android android-fragments android-radiobutton android-radiogroup

由于我是android的新手,请您确定我的代码中做错了什么,不胜感激,如果您需要更多信息,请告诉我。

实际上,我正在进行动态Radio组的创建,尽管我正在使用诸如表格/滑动布局之类的片段在视图寻呼机上

我为清除单选组/单选按钮所做的一切

我尝试了radioGroup.clearCheck();,但是在这种情况下,我遇到了一个循环问题,因此我跳过了这个问题,并想取消选中当前正在收听的广播组的所有按钮,但是在这样做时,我却面临着不可预测的问题行为,例如有些时候我无法检查另一个弹药或可以检查两个按钮

请注意我正在尝试的操作:只是我更改了某些内容,想要什么,在收音机组中有一组按钮,并且当我单击一个按钮时,它被选中了,但是我单击了在同一广播组的另一个按钮上,则先前检查的按钮不会被取消选中,甚至新的按钮也不会被选中,所以我所做的是,当被检查的是调用并且代码到达我的监听器时,我正在检查按钮是否已被选中,然后我正在尝试取消相同的检查,是否有任何文档或其他东西可以用来动态创建大量无线电组并进行处理。我相信,因为我正在动态创建按钮,所以我面临着这个问题

  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);

    }

0 个答案:

没有答案