1

我需要以编程方式取消选中复选框,但检查列表在对话框中。这会改变解决方案吗?到目前为止我所拥有的:

CharSequence[] items = itemList.toArray(new CharSequence[itemList.size()]);

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select categories");

builder.setMultiChoiceItems(items, null,
new DialogInterface.OnMultiChoiceClickListener() {

public void onClick(DialogInterface dialog, int which, boolean isChecked) {
  if (isChecked){
    if(chosenCat.size() < 10){
      chosenCat.add(items[which].toString());
    }else{
      Toast.makeText(getApplicationContext(), "Max 10 categories to compare.",   Toast.LENGTH_SHORT).show();
      //uncheck
      items.isChecked = false; // --> wrong
      chosenCat.remove(items[which].toString());
    }
  }else{
    chosenCat.remove(items[which].toString());
  }
}
});
4

2 回答 2

4

您可以随时使用.setChecked(false);

例子:

CheckBox myCheckBox = (CheckBox) findViewById(R.id.myCheckBox);

myCheckBox.setChecked(false); // false = uncheck it, true = check it
于 2013-05-22T14:32:22.100 回答
2

CheckBox同时具有isChecked ()setChecked()方法。这些足以实现您想要的。

if (checkBox.isChecked()) {
   checkBox.setChecked(false);
}
于 2013-05-22T14:34:50.213 回答