在动态添加动态android之后,单选按钮全部可在无线电组中选择

时间:2014-03-05 20:27:20

标签: android radio-button radio-group

我一直在尝试使用解决方案here来模拟表格布局中两列宽的广播组,并且有7个条目。在动态添加单选按钮后,我在一个RadioGroup列中有4个,在另一个RadioGroup列中有3个。

不幸的是,我的代码无效。在动态添加单选按钮后,第一组单选按钮似乎独立运行=>第一列(radiogroup1)可以选择所有单选按钮。第2列(radiogroup2)您一次只能选择一个单选按钮。

这是代码(没有关于Global Vars的评论,这是我正在修改的遗留代码):

    /****Modified to use two radio groups that act like one radio group ***/

private void setupRadioButtonAnswers() {
    ArrayList<HolderAnswer> listAnswers = GlobalVars.questionHolders[GlobalVars.arrayRowNumber].getListAnswers();
    int NumberInColumns = (int)Math.floor(listAnswers.size()/NUMBER_OF_COLUMNS);
    //make higher number of answers on the right
    if (listAnswers.size()% NUMBER_OF_COLUMNS > 0) NumberInColumns++;
    int count = 0;

    for (HolderAnswer answer : listAnswers) {
        if (count==NumberInColumns && RadioGroup1) { 
            RadioGroup1 = false;
            count = 0;
        }
        final AssistekRadioButton button = new AssistekRadioButton(this);
        button.setTag(answer.getRecID());
        button.setId(GlobalVars.getLatestId());
        button.setTextColor(Color.BLACK);
        GlobalVars.setupText(con, button, answer.getTextID());

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                button.setEnabled(false);
                handlerReenableView.sendEmptyMessageDelayed(button.getId(), 1000);

                button.setChecked(true);
                if (RadioGroup1) { 
                    radioGroup.check(button.getId());
                    //radioGroup2.clearCheck();
                }
                else {
                    radioGroup2.check(button.getId()); 
                    //radioGroup.clearCheck();

                }
            }
        });



        if (answer.getOption(optOther) != null) {
         button.setAllowOtherET(true, answer);
        } else if (answer.getOption(optOtherET) != null) {
         button.setAllowOtherET(true, answer);
        } else {
         button.setAllowOtherET(false, null);
        }

        if (answer.getOption(optOtherET2) != null) {
         button.setAllowOtherET2(true, answer);
        } else {
         button.setAllowOtherET2(false, null);
         }

        if (answer.getOption(optOtherDP) != null) {
         button.setAllowOtherDP(true, answer);
        } else {
         button.setAllowOtherDP(false, null);
        }

        if (RadioGroup1) {
           radioGroup.addView(button);
        }
        else { 
            radioGroup2.addView(button);
        }
        count++;
    }

    listener1 = new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            if (checkedId != -1) {
                radioGroup2.setOnCheckedChangeListener(null); // remove the listener before clearing so we don't throw an exception
                radioGroup2.clearCheck(); // clear the second RadioGroup!
                radioGroup2.setOnCheckedChangeListener(listener2); //reset the listener
                for (int i = 0; i < radioGroup.getChildCount(); i++) {
                    if (AssistekRadioButton.class.isAssignableFrom(radioGroup.getChildAt(i).getClass())) {
                        ((AssistekRadioButton)radioGroup.getChildAt(i)).setChecked(false);
                    }
                }

            }
            else { 
                for (int i = 0; i < radioGroup.getChildCount(); i++) {
                    if (AssistekRadioButton.class.isAssignableFrom(radioGroup.getChildAt(i).getClass()) && radioGroup.getChildAt(i).getId() != checkedId) {
                        ((AssistekRadioButton)radioGroup.getChildAt(i)).setChecked(false);
                    }
                }

                AssistekRadioButton checkedRadioButton = (AssistekRadioButton)radioGroup.findViewById(checkedId);
                checkedRadioButton.setChecked(true);
            }
        }
    };

    listener2 = new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            if (checkedId != -1) {
                radioGroup.setOnCheckedChangeListener(null);
                radioGroup.clearCheck();
                radioGroup.setOnCheckedChangeListener(listener1);
                for (int i = 0; i < radioGroup2.getChildCount(); i++) {
                    if (AssistekRadioButton.class.isAssignableFrom(radioGroup2.getChildAt(i).getClass())) {
                        ((AssistekRadioButton)radioGroup2.getChildAt(i)).setChecked(false);
                    }
                }

            }
            else { 
                for (int i = 0; i < radioGroup2.getChildCount(); i++) {
                    if (AssistekRadioButton.class.isAssignableFrom(radioGroup2.getChildAt(i).getClass()) && radioGroup2.getChildAt(i).getId() != checkedId) {
                        ((AssistekRadioButton)radioGroup2.getChildAt(i)).setChecked(false);
                    }
                }

                AssistekRadioButton checkedRadioButton = (AssistekRadioButton)radioGroup2.findViewById(checkedId);
                checkedRadioButton.setChecked(true);

            }
        }
    };
    radioGroup.setOnCheckedChangeListener(listener1);
    radioGroup2.setOnCheckedChangeListener(listener2);




}

和xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/transitionForm"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/backgroundcolor"
android:orientation="horizontal" >

<include
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    layout="@layout/assistekborders" />

<include
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    layout="@layout/assistek_pro_borderwidgets" />

<include
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    layout="@layout/assistek_pro_questionlabels" />

<include
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    layout="@layout/assistek_pro_practicesessionwidgets" />

<include
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    layout="@layout/assistek_pro_progress" />

<ScrollView
    android:id="@+id/svPRORGWidgets"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/ImageBottom"
    android:layout_below="@+id/lblSubQuestionText"
    android:layout_marginBottom="30px"
    android:layout_marginRight="50px"
    android:layout_marginTop="30px"
    android:layout_toRightOf="@+id/lblQuestionNumber"
    android:descendantFocusability="beforeDescendants" >
    <LinearLayout 
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"> 

    <TableLayout 
      android:id="@+id/tableLayout1"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent">
        <TableRow
    android:id="@+id/tableRow1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="5dip" >
     -->
    <RadioGroup
        android:id="@+id/radioGroup"
        android:layout_width="fill_parent"
        android:layout_column="1"
        android:layout_height="wrap_content" >
    </RadioGroup>
    <LinearLayout 
   android:layout_width="10dp"
   android:layout_height="wrap_content"
   android:layout_column="2"
   android:orientation="horizontal">
   </LinearLayout>
     <RadioGroup
        android:id="@+id/radioGroup2"
        android:layout_width="fill_parent"
        android:layout_column="3"
        android:layout_height="wrap_content" >
    </RadioGroup>
    </TableRow>
    </TableLayout>
    </LinearLayout>
</ScrollView>

有人跳出来吗?

1 个答案:

答案 0 :(得分:0)

工作代码以防任何人需要它:

/**** Modified to use two radio groups that act like one radio group ***/

private void setupRadioButtonAnswers() {
    ArrayList<HolderAnswer> listAnswers = GlobalVars.questionHolders[GlobalVars.arrayRowNumber].getListAnswers();

    int NumberInColumns = (int)Math.floor(listAnswers.size() / NUMBER_OF_COLUMNS);
    // make higher number of answers on the right
    if (listAnswers.size() % NUMBER_OF_COLUMNS > 0)
        NumberInColumns++;
    int count = 0;

    for (HolderAnswer answer : listAnswers) {
        if (count == NumberInColumns && RadioGroup1) {
            RadioGroup1 = false;
            count = 0;
        }
        final AssistekRadioButton button = new AssistekRadioButton(this);
        button.setTag(answer.getRecID());
        button.setId(GlobalVars.getLatestId());
        button.setTextColor(Color.BLACK);
        GlobalVars.setupText(con, button, answer.getTextID());

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                button.setEnabled(false);
                handlerReenableView.sendEmptyMessageDelayed(button.getId(), 1000);

                button.setChecked(true);

                if (IsRadioButtonInGroup(radioGroup, button)) {
                    radioGroup.check(button.getId());
                }
                if (IsRadioButtonInGroup(radioGroup2, button)) {
                    radioGroup2.check(button.getId());
                }
                //don't erase - needed for second group of radio buttons
                button.setChecked(true);

            }
        });

        if (answer.getOption(optOther) != null) {
            button.setAllowOtherET(true, answer);
        } else if (answer.getOption(optOtherET) != null) {
            button.setAllowOtherET(true, answer);
        } else {
            button.setAllowOtherET(false, null);
        }

        if (answer.getOption(optOtherET2) != null) {
            button.setAllowOtherET2(true, answer);
        } else {
            button.setAllowOtherET2(false, null);
        }

        if (answer.getOption(optOtherDP) != null) {
            button.setAllowOtherDP(true, answer);
        } else {
            button.setAllowOtherDP(false, null);
        }

        if (RadioGroup1) {
            radioGroup.addView(button);
        } else {
            radioGroup2.addView(button);
        }
        Log.d("GlobalVars",String.valueOf(GlobalVars.aAnswers[GlobalVars.currentQuestionNumber]));
        Log.d("button Tag",String.valueOf(button.getTag()));

        if (String.valueOf(GlobalVars.aAnswers[GlobalVars.currentQuestionNumber]).equalsIgnoreCase( String.valueOf(button.getTag()))) {
            button.setChecked(true);
            //wonky kludgey code, but needed to make it work. 
            if (IsRadioButtonInGroup(radioGroup2, button)) {
                radioGroup2.check(button.getId());
            }
        }

        count++;
    }
    RadioGroup1 = true;

    listener1 = new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            if (checkedId != -1) {
                radioGroup2.setOnCheckedChangeListener(null); // remove the listener before clearing so we don't throw an exception
                radioGroup2.clearCheck(); // clear the second RadioGroup!
                radioGroup2.setOnCheckedChangeListener(listener2); // reset the listener
                Log.d("Listener", "listener1");

                for (int i = 0; i < radioGroup.getChildCount(); i++) {
                    Log.d("radioButtonID", (Integer.toString(radioGroup.getChildAt(i).getId())));
                    Log.d("checkedId", (Integer.toString(checkedId)));
                    Log.d("isChecked", Boolean.toString(((AssistekRadioButton)radioGroup.getChildAt(i)).isChecked()));
                    if (AssistekRadioButton.class.isAssignableFrom(radioGroup.getChildAt(i).getClass()) && radioGroup.getChildAt(i).getId() != checkedId) {
                        ((AssistekRadioButton)radioGroup.getChildAt(i)).setChecked(false);
                    }
                }
                // Log.d("Listener","RadioGroup2");
                UnCheckRadioButtons(radioGroup2);

            } else {
                for (int i = 0; i < radioGroup.getChildCount(); i++) {
                    if (AssistekRadioButton.class.isAssignableFrom(radioGroup.getChildAt(i).getClass()) && radioGroup.getChildAt(i).getId() != checkedId) {
                        ((AssistekRadioButton)radioGroup.getChildAt(i)).setChecked(false);
                    }
                }

                AssistekRadioButton checkedRadioButton = (AssistekRadioButton)radioGroup.findViewById(checkedId);
                checkedRadioButton.setChecked(true);
            }
        }
    };

    listener2 = new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            if (checkedId != -1) {
                radioGroup.setOnCheckedChangeListener(null);
                radioGroup.clearCheck();
                radioGroup.setOnCheckedChangeListener(listener1);
                Log.d("Listener", "listener2");
                for (int i = 0; i < radioGroup2.getChildCount(); i++) {
                    Log.d("radioButtonID", (Integer.toString(radioGroup.getChildAt(i).getId())));
                    Log.d("checkedId", (Integer.toString(checkedId)));
                    Log.d("isChecked", Boolean.toString(((AssistekRadioButton)radioGroup.getChildAt(i)).isChecked()));
                    if (AssistekRadioButton.class.isAssignableFrom(radioGroup2.getChildAt(i).getClass()) && radioGroup.getChildAt(i).getId() != checkedId) {
                        ((AssistekRadioButton)radioGroup2.getChildAt(i)).setChecked(false);

                    }
                }
                // Log.d("Listener","RadioGroup1");
                UnCheckRadioButtons(radioGroup);

            } else {
                for (int i = 0; i < radioGroup2.getChildCount(); i++) {
                    if (AssistekRadioButton.class.isAssignableFrom(radioGroup2.getChildAt(i).getClass()) && radioGroup2.getChildAt(i).getId() != checkedId) {
                        ((AssistekRadioButton)radioGroup2.getChildAt(i)).setChecked(false);
                    }
                }

                AssistekRadioButton checkedRadioButton = (AssistekRadioButton)radioGroup2.findViewById(checkedId);
                checkedRadioButton.setChecked(true);

            }
        }
    };

    radioGroup.setOnCheckedChangeListener(listener1);
    radioGroup2.setOnCheckedChangeListener(listener2);

}

// to uncheck all radio buttons in a group without setting off the
// onCheck change listener
private void UnCheckRadioButtons(RadioGroup rg) {
    int count = rg.getChildCount();
    for (int i = 0; i < count; i++) {
        AssistekRadioButton arb = (AssistekRadioButton)rg.getChildAt(i);
        arb.setChecked(false);
    }
}

private boolean IsRadioButtonInGroup(RadioGroup rg, AssistekRadioButton rb) {
    int count = rg.getChildCount();
    for (int i = 0; i < count; i++) {
        AssistekRadioButton arb = (AssistekRadioButton)rg.getChildAt(i);
        if (arb == rb) {
            return true;
        }
    }
    return false;
}
// returns the one checked value in the two radio groups.
private int returnResourceID() {

    int chkId1 = radioGroup.getCheckedRadioButtonId();
    int chkId2 = radioGroup2.getCheckedRadioButtonId();
    return chkId1 == -1 ? chkId2 : chkId1;

}