Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 动态添加单选按钮后,可在单选组中选择所有单选按钮_Android_Radio Button_Radio Group - Fatal编程技术网

Android 动态添加单选按钮后,可在单选组中选择所有单选按钮

Android 动态添加单选按钮后,可在单选组中选择所有单选按钮,android,radio-button,radio-group,Android,Radio Button,Radio Group,我一直在尝试这个解决方案来模拟一个广播组,它在一个表布局中有两列宽,有7个条目。将单选按钮动态添加到组后,我在一个RadioGroup列中有4个,在另一个RadioGroup列中有3个 不幸的是,我的代码不起作用。动态添加单选按钮后,第一组单选按钮似乎独立运行=>column one(RadioGroup 1)可以选择所有单选按钮。第2列(RadioGroup 2)一次只能选择一个单选按钮 以下是代码(请不要对全局变量发表评论,这是我正在修改的遗留代码): /****修改为使用两个无线电组,其作

我一直在尝试这个解决方案来模拟一个广播组,它在一个表布局中有两列宽,有7个条目。将单选按钮动态添加到组后,我在一个RadioGroup列中有4个,在另一个RadioGroup列中有3个

不幸的是,我的代码不起作用。动态添加单选按钮后,第一组单选按钮似乎独立运行=>column one(RadioGroup 1)可以选择所有单选按钮。第2列(RadioGroup 2)一次只能选择一个单选按钮

以下是代码(请不要对全局变量发表评论,这是我正在修改的遗留代码):

/****修改为使用两个无线电组,其作用类似于一个无线电组***/
私有无效设置RadioButtonSwers(){
ArrayList listAnswers=GlobalVars.questionHolders[GlobalVars.ArrayRownNumber].getListAnswers();
int NumberInColumns=(int)Math.floor(listAnswers.size()/NUMBER\u OF\u COLUMNS);
//在右边做更多的答案
如果(listAnswers.size()%NUMBER_OF_COLUMNS>0)NumberInColumns++;
整数计数=0;
对于(持有者回答:列表回答){
如果(count==NumberInColumns&&RadioGroup 1){
第1组=假;
计数=0;
}
最终助理KradioButton=新助理KradioButton(本);
setTag(answer.getRecID());
setId(GlobalVars.getLatestId());
按钮。setTextColor(颜色。黑色);
setupText(con、button、answer.getTextID());
setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
按钮。设置启用(错误);
handlerReenableView.sendEmptyMessageDelayed(button.getId(),1000);
按钮。setChecked(true);
如果(第1组){
radioGroup.check(button.getId());
//放射组2.clearCheck();
}
否则{
radiogroup 2.check(button.getId());
//radioGroup.clearCheck();
}
}
});
if(answer.getOption(optOther)!=null){
按钮。设置AllowTheret(正确,回答);
}else if(answer.getOption(optOtherET)!=null){
按钮。设置AllowTheret(正确,回答);
}否则{
setAllowTheret(false,null);
}
if(answer.getOption(optoret2)!=null){
按钮。设置AllowTheret2(正确,回答);
}否则{
setAllowTheret2(false,null);
}
if(answer.getOption(optOtherDP)!=null){
按钮。设置AllowotherDP(正确,回答);
}否则{
setAllowOtherDP(false,null);
}
if(第1组){
radioGroup.addView(按钮);
}
否则{
RadioGroup 2.添加视图(按钮);
}
计数++;
}
listener1=新建OnCheckedChangeListener(){
@凌驾
检查更改后的公共无效(RadioGroup组,int checkedId){
如果(检查EDID!=-1){
radioGroup2.setOnCheckedChangeListener(null);//清除前删除侦听器,这样我们就不会引发异常
RadioGroup 2.clearCheck();//清除第二个RadioGroup!
radioGroup2.setOnCheckedChangeListener(listener2);//重置侦听器
对于(int i=0;i
以及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>

-->


是否有任何东西向任何人跳出来?

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

/**** 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;

}
/****修改为使用两个r
/**** 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;

}