【问题标题】:How to do something when a checkbox change state?当复选框更改状态时如何做某事?
【发布时间】:2012-07-05 03:16:14
【问题描述】:

这是我的代码:

    <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”,但我不能这样做,有人可以帮我一些代码吗? 非常感谢各位!

【问题讨论】:

  • ""我尝试使用".setOnCheckedChangeListener",但我不能这样做""为什么你不能这样做?您的setOnCheckedChangeListener的地址代码
  • 检查过的更改监听器是做你所说的事情的方法。
  • 为什么不使用单选按钮。 (即广播组)。这正是制造这些的原因。只能使用一张,不需要禁用另外两张。顺便说一句,onCheckChangedListener 也适用于单选按钮。

标签: android checkbox oncheckedchanged android-checkbox


【解决方案1】:

这是通知您检查更改的方式:

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;

            }

}

【讨论】:

  • 谢谢。您的代码在从复选框获取布尔值时解决了我的问题。
【解决方案2】:

我相信 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
}

在这种情况下,您不必处理禁用其他选项的问题。默认情况下,用户只有一个选项可供选择。

【讨论】:

    【解决方案3】:
    @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。希望它有效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-09
      • 2021-12-19
      • 2012-01-15
      • 2015-04-10
      • 1970-01-01
      • 1970-01-01
      • 2021-03-13
      相关资源
      最近更新 更多