2016-04-07 21 views
0

我有三個Switches。我想要的是當我點擊另外兩個變成殘疾人。禁用所有其他開關當啓用一個

我所做的是將一個setCheckedListener設置爲它們中的每一個,當我檢查一個時,我將其他兩個的選中狀態設置爲false。

的代碼是:

@Override 
public void onCheckedChanged(CompoundButton compoundButton, boolean b) { 
    switch (compoundButton.getId()) { 
     case R.id.switch_most_popular: 
      mostViewedSwitch.setChecked(false); 
      latestUpdatedSwitch.setChecked(false); 
      break; 
     case R.id.switch_most_viewed: 
      mostPopularSwitch.setChecked(false); 
      latestUpdatedSwitch.setChecked(false); 
      break; 
     case R.id.switch_latest_updated: 
      mostPopularSwitch.setChecked(false); 
      mostViewedSwitch.setChecked(false); 
      break; 
     default: 
      Log.i(TAG, "no checked Switch "); 
      break; 
    } 
} 

與此代碼會發生什麼情況是,我需要時間去觸摸開關。第一次禁用那個是被選中的,第二次是啓用我檢查過的。

我應該更改上面的代碼,以便實現我想要的行爲?或者有沒有其他的選擇用於這個用例? 感謝

+0

請參閱下面的答案。 –

+0

哈哈問題。你嘗試使用谷歌? – GensaGames

回答

1

爲禁用開關使用該

mostViewedSwitch.setEnabled(false); 
latestUpdatedSwitch.setEnabled(false); 

mostViewedSwitch.setChecked(false); 
latestUpdatedSwitch.setChecked(false); 

因爲它不是複選框它的開關。

+0

@很高興幫助你。 –

相關問題