2013-02-05 28 views
1

我有2個無線電組,每個無線電組包含2個Radiobutton。我試圖在這兩組之間切換,但迄今爲止不成功。任何幫助,將不勝感激。如何在Android中的2個RadioGroups之間切換?


 myGroup2 = (RadioGroup) findViewById(R.id.radioGroup2); 
     myGroup3 = (RadioGroup) findViewById(R.id.radioGroup3); 
     myGroup3.clearCheck(); 
     myGroup2.clearCheck(); 

     myGroup2.setOnCheckedChangeListener(this); 
     myGroup3.setOnCheckedChangeListener(this); 


myGroup2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 
      // @Override 
      public void onCheckedChanged(RadioGroup group, int checkedId) { 

        myGroup3.clearCheck(); 
      } 
     }); 
    myGroup3.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 
      @Override 
      public void onCheckedChanged(RadioGroup group, int checkedId) { 

        myGroup2.clearCheck(); 
      } 
     }); 
+0

可以展開你要實現的目標到底是什麼?如果你的意思是當一個組中的一個按鈕被選中時取消選中一個組,則上述操作將不起作用。它會調用clearCheck();在兩個組上。 – anthropomo

+0

嗨anthropomo,是的,這正是我想要實現的,其中一組取消選中另一個按鈕中的一個按鈕被選中。 – user1207965

+0

你想達到什麼目的?而不是有2個無線電組爲什麼不1,並在那裏添加所有單選按鈕? –

回答

3

這裏檢查API:

http://developer.android.com/reference/android/widget/RadioGroup.OnCheckedChangeListener.html

編輯:

不樂意過了多長時間,但我想通了這一點。當組已清除時,onCheckedChanged僅爲checkedId傳遞-1。如果一個盒子沒有被選中,它的ID仍然通過。

你可以這樣做:

myGroup2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){ 
     @Override 
     public void onCheckedChanged(RadioGroup group, int checkedId) { 
      if(checkedId != -1 && 
       ((RadioButton) findViewById(checkedId)).isChecked()){ 
       myGroup3.clearCheck(); 
      } 
     } 
    }); 
    myGroup3.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){ 
     @Override 
     public void onCheckedChanged(RadioGroup group, int checkedId) { 
       if(checkedId != -1 && 
         ((RadioButton) findViewById(checkedId)).isChecked()){ 
        myGroup2.clearCheck(); 
       } 
     } 
    }); 

或者,可能多一點理智,有活動實施監聽器,這部分是onCreate方法之外:

@Override 
public void onCheckedChanged(RadioGroup group, int checkedId) { 
    if(checkedId != -1 && ((RadioButton) findViewById(checkedId)).isChecked()){ 
     if(group == myGroup2){ 
      myGroup3.clearCheck(); 
     } 
     else if(group == myGroup3){ 
      myGroup2.clearCheck(); 
     } 
     // And your switch for whatever you want to happen based on the 
     // selected button goes here 
    } 
} 
+0

嗨anthropomo,我確實嘗試了以上,但不能正常工作;當你點擊第一組時,被選中,但是當點擊第二組時,一切都被清除了,所以你必須在第二組上點擊兩次才能被選中......我爲group3做了同樣的代碼。 – user1207965

+0

我堅持下來,一切都很好。我懷疑是可能不應該在裏面:\t保護無效onCreate(Bundle savedInstanceState){...再次不確定... – user1207965

+0

我相信,有一個解決方案,所有這一切。我想到一個開關和情況下,如果它可以爲2聽衆... – user1207965

1

這裏是我想出瞭解決方案。

radioGroup1 = (RadioGroup) findViewById(R.id.radioGroup1); 
    radioGroup2 = (RadioGroup) findViewById(R.id.radioGroup2); 
    RadioButton[] tempradbutton1 = { 
      (RadioButton) findViewById(R.id.radio1), 
      (RadioButton) findViewById(R.id.radio2) }; 
    RadioButton[] tempradbutton2 = { 
      (RadioButton) findViewById(R.id.radio4), 
      (RadioButton) findViewById(R.id.radio5) }; 
    radbutton1 = tempradbutton1; 
    radbutton2 = tempradbutton2; 
    for (int i = 0; i < radbutton1.length; i++) { 
     radbutton1[i].setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       radioGroup2.clearCheck(); 
      } 
     }); 
     radbutton2[i].setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       radioGroup1.clearCheck(); 

      } 
     }); 
    } 

而是然後用setOnCheckedChangeListener,使用onClickListener每一個單選按鈕存在於RadioGroup中。

XML:

<RadioGroup 
    android:id="@+id/radioGroup1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" > 
    <RadioButton 
     android:id="@+id/radio1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="RadioButton" /> 

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

<RadioGroup 
    android:id="@+id/radioGroup2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_below="@+id/radioGroup1" > 
    <RadioButton 
     android:id="@+id/radio4" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="RadioButton" /> 

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

嗨Christian.Thanks的解決方案,但試圖找出你從哪裏得到radio0和radio3,因爲我沒有超過4個按鈕,每組2個,他們添加或隱藏單選按鈕? – user1207965

+0

radio0和radio3被添加到XML中的radiogroup中。如果您只有2個按鈕,只需刪除上面代碼中的radio0和radio3即可。 –

+0

仍然無法正常工作。 – user1207965