2013-04-26 17 views
2

我在我的活動中有一個RadioGroup,其中包含四個具有自定義背景的ToggleButton。我希望用戶能夠一次選擇任何一個按鈕,並且不應該沒有選擇任何按鈕,但即使它在RadioGroup中,並且其他所有工作都正常,選擇已選擇的ToggleButton將取消選擇它,而不會選擇任何按鈕按鈕被選中。如何強制RadioGroup中的ToggleButton在第二次按下時保持選中狀態?

如何讓用戶再次點擊ToggleButton時保持選中狀態?

我的XML:我的onCreate()的

<RadioGroup 
    android:id="@+id/radio_group" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center_horizontal" 
    android:orientation="horizontal" /> 

相關大塊:

radioGroup = (RadioGroup) findViewById(R.id.radio_group); 
radioGroup.setOnCheckedChangeListener(onRadioGroupClickListener); 

for (int index = 0; index < OPTIONS.length; index++) { 
    ToggleButton button = new ToggleButton(this); 
    button.setId(index); 
    button.setText(OPTIONS[index]); 
    button.setTextOn(OPTION[index]); 
    button.setTextOff(OPTIONS[index]); 
    button.setChecked(index == 0); // Set to first option by default 
    button.setButtonDrawable(Color.TRANSPARENT); 
    button.setBackgroundResource(R.drawable.selector); 
    button.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      RadioGroup parent = (RadioGroup) view.getParent(); 
      ToggleButton button = (ToggleButton) view; 
      TimesheetLog.d("View is checked: " + button.isChecked()); 

      parent.check(button.getId()); 
      currentSelection = view.getId(); 
     } 
    }); 

    radioGroup.addView(button); 
} 

如果我需要添加任何代碼,請告訴我。謝謝!

+1

恩,你爲什麼不使用收音機組中的單選按鈕?他們的行爲是你想要的方式 – 2013-04-26 12:04:05

+0

我有很多事情要做,並且據我所知,單選按鈕並不像真正的按鈕。我錯了嗎?我認爲文字總是偏向一邊...... – Cornholio 2013-04-26 12:25:03

+0

他們看起來像單選按鈕:-)是的,文字是偏旁的。你看看[這個問題](http://stackoverflow.com/questions/2379527/android-how-to-get-a-radiogroup-with-togglebuttons)? – 2013-04-26 12:39:41

回答

0

下面的行應該與切換點擊事件在同一個類中;

private RadioButton mBtnCurrentRadio; 

切換點擊事件(應該在活動中);

public void onToggle(View view) { 

     final ToggleButton mBtnToggle = (ToggleButton) view; 

     // select only one toggle button at any given time 
     if (mBtnCurrentToggle != null) { 
      mBtnCurrentToggle.setChecked(false); 
     } 
     mBtnToggle.setChecked(true); 
     mBtnCurrentToggle = mBtnToggle; 

     // app specific stuff .. 
    } 

RadioGroup用於佈局的xml代碼;

<RadioGroup 
     android:id="@+id/toggleGroup" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="24dp" 
     android:orientation="horizontal" > 

     <ToggleButton 
      android:id="@+id/btn_Letter" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:onClick="onToggle" 
      android:textOff="Letter" 
      android:textOn="Letter" 
      style="@style/toggleBtnStyle" /> 

     <ToggleButton 
      android:id="@+id/btn_A4" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:onClick="onToggle" 
      android:textOff="A4" 
      android:textOn="A4" 
      style="@style/toggleBtnStyle" /> 

     <ToggleButton 
      android:id="@+id/btn_A3" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:onClick="onToggle" 
      android:textOff="A3" 
      android:textOn="A3" 
      style="@style/toggleBtnStyle" /> 
    </RadioGroup> 
0

我有同樣的問題,只是想通了。

使用代碼片斷你有你的問題,如果添加一行:

button.setOnClickListener(new OnClickListener() { 
    @Override 
    public void onClick(View view) { 
     RadioGroup parent = (RadioGroup) view.getParent(); 
     ToggleButton button = (ToggleButton) view; 
     TimesheetLog.d("View is checked: " + button.isChecked()); 

     // ------------------------------------------------- 
     parent.clearCheck(); // <----------- ADD THIS------- 
     // ------------------------------------------------- 
     parent.check(button.getId()); 
     currentSelection = view.getId(); 
    } 
}); 

我相信它應該工作。我認爲發生的事情是check()方法實際上是自己的切換。如果我們每次都清除檢查,那麼每次我們調用check()之後,都會保證它處於按下狀態。這使得不可能取消選中ToggleButton。

相關問題