我需要以编程方式取消选中复选框,但检查列表在对话框中。这会改变解决方案吗?到目前为止我所拥有的:
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());
}
}
});
答案 0 :(得分:4)
您始终可以使用.setChecked(false);
示例:
CheckBox myCheckBox = (CheckBox) findViewById(R.id.myCheckBox);
myCheckBox.setChecked(false); // false = uncheck it, true = check it
答案 1 :(得分:2)
CheckBox同时包含isChecked()和setChecked()方法。这些足以实现你想要的目标。
if (checkBox.isChecked()) {
checkBox.setChecked(false);
}