“选中一个单选按钮取消选中所有其他按钮”不起作用

时间:2012-10-01 10:42:50

标签: java android radio-group

我正在开发Android 3.1平板电脑应用程序,我需要以编程方式创建一些单选按钮:

    RadioGroup radGrp = new RadioGroup(mActivity);
    params.weight = 0.2f;
    radGrp.setLayoutParams(params);
    radGrp.setOrientation(RadioGroup.HORIZONTAL);

    String tag = Long.toString(req.getRequirementId()) + "_" +
            getString(R.string.yes);
    RadioButton radioBtnYES = new RadioButton(mActivity);
    radioBtnYES.setText(getString(R.string.yes));
    radioBtnYES.setTag(tag);
    radioBtnYES.setChecked(confValue);
    radioBtnYES.setOnClickListener(radioListener);
    radGrp.addView(radioBtnYES);

    tag = Long.toString(req.getRequirementId()) + "_" +
            getString(R.string.no);
    RadioButton radioBtnNO = new RadioButton(mActivity);
    radioBtnNO.setText(getString(R.string.no));
    radioBtnNO.setTag(tag);
    radioBtnNO.setChecked(!confValue);
    radioBtnNO.setOnClickListener(radioListener);
    radGrp.addView(radioBtnNO);

    radGrp.setOnCheckedChangeListener(checkedChangedListener);
    requirementLayout.addView(radGrp);

我的问题是,当我点击未经检查的单选按钮时,同一个广播组上的另一个按钮仍会被检查。

这是radioListener代码:

private RadioButton.OnClickListener radioListener = 
        new RadioButton.OnClickListener()
{
    @Override
    public void onClick(View v)
    {
        String tag = v.getTag().toString();
        String[] parts = tag.split("_");

        if ((parts != null) && (parts.length == 2))
        {
            boolean value = (parts[1].equals(getString(R.string.yes)));

            Long id = Long.valueOf(parts[0]);
            requirementsState.put(id, value);
        }
    }
};

这是checkedChangedListener代码:

private RadioGroup.OnCheckedChangeListener checkedChangedListener =
        new OnCheckedChangeListener()
{
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId)
            {
                RadioButton rb = (RadioButton)
                        mActivity.findViewById(checkedId);

                group.setOnCheckedChangeListener(null);

                group.clearCheck();
                rb.setChecked(true);

                group.setOnCheckedChangeListener(checkedChangedListener);
            }
};

我做错了什么?

1 个答案:

答案 0 :(得分:1)

我找到了答案:**我需要在将所有无线电按钮添加到无线电组后设置已检查的单选按钮。

这是我的工作代码:

    RadioGroup radGrp = new RadioGroup(mActivity);
    params.weight = 0.2f;
    radGrp.setLayoutParams(params);
    radGrp.setOrientation(RadioGroup.HORIZONTAL);

    String tag = Long.toString(req.getRequirementId()) + "_" +
            getString(R.string.yes);
    RadioButton radioBtnYES = new RadioButton(mActivity);
    radioBtnYES.setText(getString(R.string.yes));
    radioBtnYES.setTag(tag);
    radioBtnYES.setOnClickListener(radioListener);
    radGrp.addView(radioBtnYES);

    tag = Long.toString(req.getRequirementId()) + "_" +
            getString(R.string.no);
    RadioButton radioBtnNO = new RadioButton(mActivity);
    radioBtnNO.setText(getString(R.string.no));
    radioBtnNO.setTag(tag);
    radioBtnNO.setOnClickListener(radioListener);
    radGrp.addView(radioBtnNO);

我已向Radio Group添加了两个Radio Buttons,然后:

    if (confValue)
        radioBtnYES.setChecked(true);
    else
        radioBtnNO.setChecked(true);

    requirementLayout.addView(radGrp);