"إن تحديد زر اختيار واحد يؤدي إلى إلغاء تحديد جميع الأزرار الأخرى" لا يعمل

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

لقد أضفت زري راديو إلى Radio Group ثم:

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

    requirementLayout.addView(radGrp);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top