2013-12-19 56 views
2

我有一個帶有一些單選按鈕的RadioGroup。我正在使用這種方法rGroup.clearCheck();清除RadioGroup中選中的itme。問題是RadioGroup.setOnCheckedChangeListener在我使用這個方法clearCheck()後會被調用兩次。我面臨的問題是這種方法被稱爲兩次。RadioGroup的setOnCheckedChangeListener在調用RadioGroup上的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

檢查this..http://stackoverflow.com/questions/4519103/error-in-androids-clearcheck-for-radiogroup –

+0

這裏發表您的XML文件。 – Piyush

+0

@piyush我已經包含佈局文件 – ravi

回答

0

試試這個...

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(); 
     } 
+0

我已經包含了佈局文件。 – ravi

+0

在哪裏調用clearRadio()? –