在RadioGroup上调用clearCheck()后,RadioGroup的setOnCheckedChangeListener被调用两次

时间:2013-12-19 07:54:54

标签: android radio-group

我有一个带有单选按钮的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>

提前致谢。

1 个答案:

答案 0 :(得分:-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();
        }