如何取消选中已选中的复选框?

How to unchecked the checked checkbox?

得到勾选结果后如何清除勾选框?

类似于 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);
    }
}

你可以使用

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.

使用setChecked():

checkBox.setChecked(true/false)

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

检查:

checkBox.setChecked(true);

并取消选中:

checkBox.setChecked(false);

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

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

checkbox.toggle();

否则

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