【问题标题】:How to unchecked the checked checkbox?如何取消选中已选中的复选框?
【发布时间】:2016-09-21 11:09:07
【问题描述】:

我们得到检查结果后如何清除复选框?

类似selection.clear(); 的东西,但只会清除输出,不会清除复选框。

我要做的是,将复选框设置为未选中的原始状态。

在用户选中复选框然后单击按钮以获取选中复选框的结果后,我希望清除复选框中的所有选择。请问如何帮忙?

public class DessertIngAvail extends Dessert {

ArrayList<String> selection = new ArrayList<String>();
TextView final_text;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dessert_ing_avail);

    final_text = (TextView)findViewById(R.id.final_result);
    final_text.setEnabled(false);
}

public void selectItem(View view){
    boolean checked = ((CheckBox) view).isChecked();
    switch (view.getId()) {
         case R.id.checkBox181:
            if(checked) {
                if(!selection.contains("Tebaloi"))
                    selection.add("Tebaloi");

                if(!selection.contains("Tumpik"))
                    selection.add("Tumpik");
            }

            break;

        case R.id.checkBox182:
            if(checked) {
                if(!selection.contains("Ambuyat"))
                    selection.add("Ambuyat");
            }

            break;

        case R.id.checkBox183:
            if(checked) {
                if(!selection.contains("Tumpik"))
                    selection.add("Tumpik");
            }

            break;

        case R.id.checkBoxCM:
            if(checked) {

                if(!selection.contains("Honey Frankincense Cake"))
                    selection.add("Honey Frankincense Cake");

                if(!selection.contains(" Ray Heart Cake"))
                    selection.add(" Ray Heart Cake");
            }

            break;
    }
}

public void finalSelection(View view) {

    String final_fruit_selection = "";

    for(String Selection : selection) {
        final_fruit_selection = final_fruit_selection + Selection + "\n";
    }

    final_text.setText(final_fruit_selection);
    selection.clear();

    final_text.setEnabled(true);
    }
}

【问题讨论】:

    标签: java android android-studio checkbox android-widget


    【解决方案1】:

    如果你确定复选框被选中,你可以切换它们。

    checkbox.toggle();
    

    否则

    checkBox.setChecked(false); // pass true if you want to select.
    

    【讨论】:

      【解决方案2】:

      你可以使用

      checkBox.setChecked(boolean);
      //to clear the check box
      checkBox.setChecked(false);
      

      更新了 finalSelection(View view) 方法;

      public void finalSelection(View view){
          String final_fruit_selection = "";
          for(String Selection : selection){
             final_fruit_selection = final_fruit_selection + Selection + "\n";
          }
          final_text.setText(final_fruit_selection);
          selection.clear();
          final_text.setEnabled(true);
      
          //now clear checkboxes
          checkBox181.setChecked(false);
          checkBox182.setChecked(false);
          checkBox183.setChecked(false);
          checkBoxCM.setChecked(false);
      }
      

      改变非常非常非常基本的东西

      //right below TextView final_text; at the top add this
      CheckBox checkBox181,checkBox182,checkBox183,checkBoxCM;
      
      //declare then in onCreate()
      @Override
      protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_dessert_ing_avail);
      
         final_text = (TextView)findViewById(R.id.final_result);
         final_text.setEnabled(false);
         checkBox181=(CheckBox)findViewById(R.id.checkBox181);
         checkBox182=(CheckBox)findViewById(R.id.checkBox182);
         checkBox183=(CheckBox)findViewById(R.id.checkBox183);
         checkBoxCM=(CheckBox)findViewById(R.id.checkBoxCM);
      }
      

      我建议你在线跟进一些java课程。 This 是一个很好的起点。

      开始学习android开发请查看here

      【讨论】:

      • 我应该在哪里实现该行?在public void final selection ?
      • 是的。你可以在任何你想要的地方做。但是是的,你完全可以在 final_selection 方法上做到这一点。
      • 我需要申报什么吗?因为它无法解析符号复选框
      • 它是您代码中的任何复选框,例如 ckeckBox183
      • 对不起,我不明白..我是新手
      【解决方案3】:

      我不确定我是否理解您的问题,但您可以使用checkBox.setChecked(false) 设置一个复选框。要取消选中所有选中的复选框,只需循环它们并取消选中选中的复选框。

      【讨论】:

      • 如何遍历它们?抱歉,我是这方面的新手
      • 如果这些复选框是唯一的,则不需要循环。只需执行四次checkBox.setChecked(false)。而不是checkBox 使用(CheckBox) findViewById(R.id.my_checkbox)
      • 实际上那些不是完整的代码..我有四个以上的复选框。许多复选框。又怎样?我必须按 ID 声明它们中的每一个吗?
      • 您是否使用列表适配器来执行此操作?
      • 那恐怕你得把它们都找到ViewById了。考虑将来使用适配器。
      【解决方案4】:

      检查:

      checkBox.setChecked(true);
      

      并取消选中:

      checkBox.setChecked(false);
      

      【讨论】:

        【解决方案5】:

        使用setChecked():

        checkBox.setChecked(true/false)
        

        文档:https://developer.android.com/reference/android/widget/CheckBox.html

        【讨论】:

          猜你喜欢
          • 2012-10-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-08-02
          • 2011-02-27
          • 2020-03-16
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多