2

我正在开发一个 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);

我的问题是,当我点击一个未选中的单选按钮时,同一个 Radio Group 上的另一个按钮仍然处于选中状态。

这是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);
            }
};

我究竟做错了什么?

4

1 回答 1

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 中添加了两个单选按钮,然后:

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

    requirementLayout.addView(radGrp);
于 2012-10-01T17:25:50.267 回答