"라디오 버튼 하나를 선택하면 다른 모든 버튼의 선택이 취소됩니다"가 작동하지 않습니다

StackOverflow https://stackoverflow.com//questions/12671746

  •  12-12-2019
  •  | 
  •  

문제

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

내가 도대체 ​​뭘 잘못하고있는 겁니까?

도움이 되었습니까?

해결책

나는 답을 찾았습니다:**모든 라디오 버튼을 라디오 그룹에 추가한 후 체크된 라디오 버튼을 설정해야 합니다.

이것은 내 작업 코드입니다.

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

라디오 그룹에 두 개의 라디오 버튼을 추가한 후 다음을 수행했습니다.

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

    requirementLayout.addView(radGrp);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top