setOnCheckedChangeListener RadioGroup вызывается дважды после вызова clearCheck() для RadioGroup

У меня есть RadioGroup с переключателями. Чтобы очистить отмеченный элемент, я звоню radiogroup.clearCheck(). Но обратный вызов RadioGroup.setOnCheckedChangeListener запускается дважды после вызова clearCheck().

public class MainActivity extends Activity {
        private RadioGroup rGroup;
        private String str;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            rGroup = (RadioGroup) findViewById(R.id.radioGroup1);
            rGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    // TODO Auto-generated method stub
                    if (checkedId == R.id.radio0) {
                        str = "Radio0";
                    } else if (checkedId == R.id.radio1) {
                        str = "Radio1";
                    } else if (checkedId == R.id.radio2) {
                        str = "Radio2";
                    }
                }
            });
        }

        public void clearRadio(View v) {
            rGroup.clearCheck();
        }

    }

XML-файл

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginLeft="48dp"
        android:layout_marginTop="58dp" >

        <RadioButton
            android:id="@+id/radio0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="RadioButton1" />

        <RadioButton
            android:id="@+id/radio1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="RadioButton2" />

        <RadioButton
            android:id="@+id/radio2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="RadioButton3" />
    </RadioGroup>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/radioGroup1"
        android:onClick="clearRadio"
        android:text="Clear" />

</RelativeLayout>

Заранее спасибо.


person ravi    schedule 19.12.2013    source источник
comment
Проверьте это.. stackoverflow.com/questions/ 4519103/   -  person Jagadesh Seeram    schedule 19.12.2013
comment
разместите здесь свой xml-файл.   -  person Piyush    schedule 19.12.2013


Ответы (1)


попробуй это...

public class MainActivity extends Activity {

        private RadioGroup rGroup;
        private String str;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            rGroup = (RadioGroup) findViewById(R.id.radioGroup1);
            rGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    // TODO Auto-generated method stub
                    if (checkedId == R.id.radio0) {
                        str = "Radio0";
                    } else if (checkedId == R.id.radio1) {
                        str = "Radio1";
                    } else if (checkedId == R.id.radio2) {
                        str = "Radio2";
                    }
                }
            });
        }


    }

 public void clearRadio(View v) {
            rGroup.clearCheck();
        }
person saa    schedule 19.12.2013