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" />

私がやりたいのは、これらの1つがチェックされたときに「検出」し、他の2つを「無効」に設定して、ユーザーが一度に1つしか選択できないようにすることです。「.setOnCheckedChangeListener」を使用しようとしましたが、使用できません。誰かがコードを手伝ってくれますか?どうもありがとう!

4

3 に答える 3

29

これは、チェックされた変更について通知される方法です。

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;

            }

}
于 2012-07-04T15:51:46.263 に答える
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 つだけです。

于 2012-07-04T16:53:03.367 に答える