2012-07-04 119 views
11

這是我的代碼:複選框更改狀態時如何做某些事情?

<CheckBox 
     android:id="@+id/sprint_checkbox" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/sprint_game" /> 

    <CheckBox 
     android:id="@+id/marathon_checkbox" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/marathon" /> 

    <CheckBox 
     android:id="@+id/never_ending_checkbox" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/never_ending" /> 

我想要做的是「檢測」時,其中一個被選中,然後將另外兩個爲「禁用」,這樣用戶就可以在時間只有一個選擇。 我試圖使用「.setOnCheckedChangeListener」,但我不能這樣做,有人可以幫我一些代碼? 非常感謝你們!

+0

」「我試圖使用」.setOnCheckedChangeListener「,但我不能那樣做」「 爲什麼你不能這樣做?你的'setOnCheckedChangeListener'的地方代碼 –

+0

選中的變更監聽器是做你正在談論的方法。 – matt5784

+2

爲什麼不使用單選按鈕。 (即無線電組)。這正是爲什麼這些是製造的。使用只能照一張,不需要禁用另外兩張。順便說一下,onCheckChangedListener也可以在單選按鈕上工作。 – drulabs

回答

25

這是你通知有關檢查的變更方式:

CheckBox check = findViewById(R.id.sprint_checkbox); 
check.setOnCheckedChangeListener(new OnCheckedChangeListener() { 

     @Override 
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
      //do stuff 

     } 
    }); 

您也可以讓您的活動實現了OnCheckedChangeListener接口,然後:

CheckBox check1 = findViewById(R.id.sprint_checkbox); 
CheckBox check2 = findViewById(R.id.marathon_checkbox); 
CheckBox check3 = findViewById(R.id.never_ending_checkbox); 

check1.setOnCheckedChangeListener(this); 
check2.setOnCheckedChangeListener(this); 
check3.setOnCheckedChangeListener(this); 

重寫接口的方法:

@Override 
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
    switch(buttonView.getId()){ 
       case R.id.sprint_checkbox: 
       //do stuff 
       break; 
       case R.id.marathon_checkbox: 
       //do stuff 
       break; 
       case R.id.never_ending_checkbox: 
       //do stuff 
       break; 

      } 

} 
+0

非常感謝! – hyperloris

+1

謝謝。您的代碼在從複選框獲取布爾值時解決了我的問題。 – BK19

1

我相信RadioButton會更適合您的目標。

<RadioGroup 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:id="@+id/radio_group1"> 
    <RadioButton 
     android:id="@+id/sprint" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/rad_option1" 
     android:checked="true" 
    android:onClick="onRadioButtonClick" 
     /> 
    <RadioButton 
     android:id="@+id/marathon" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/rad_option2" 
     android:onClick="onRadioButtonClick" 
     /> 
    <RadioButton 
     android:id="@+id/neverending" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/rad_option3" 
     android:onClick="onRadioButtonClick" 
     /> 
</RadioGroup> 

然後在你的代碼:

public void onRadioButtonClick(View v) { 
    RadioButton button = (RadioButton) v; 
    // DO YOUR THING HERE DEPENDING ON WHICH RADIOBUTTON IS PICKED 
} 

在這種情況下,你將不必應付禁用其他選項。用戶默認只有一個選項可供選擇。

1
@Override 
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 

    int id = buttonView.getId(); 

    if(isChecked){ 
     switch(id){ 
      case R.id.sprint: 
      //add to database 
      ... 
     } 
    } 
    else{ 
     switch(id){ 
      case R.id.sprint: 
      //remove from database 
      ... 
     } 
    } 
} 

這應該允許你帶ID。希望它有效。 「

相關問題