gpt4 book ai didi

android - 无法在 onPrepareDialog 中设置复选框状态

转载 作者:行者123 更新时间:2023-11-29 22:30:49 27 4
gpt4 key购买 nike

我有一个显示复选框列表的对话框。每次打开时应检查哪些框的信息来自编辑文本。我搜索了一种不允许缓存对话框的方法,但找不到该怎么做。现在我覆盖 onPrepareDialog 以在对话框打开之前设置复选框。我删除了我的 edittext 的内容,打开了对话框,但仍然选中了相同的框...谁能告诉我如何重置复选框?

    @Override
protected void onPrepareDialog(int id, Dialog dialog) {
ListView lv = ((AlertDialog) dialog).getListView();

if (lv == null)
return;

boolean[] checked = cbDialog.setAndGetCheckedArray();

String s = "onPrepareDialog... checked=";
for (int i=0; i<checked.length; i++)
s+="["+i+"="+checked[i]+"]";
System.out.println(s);

// if edittext is empty, all entries in checked[] are false here,
// but these changes do NOT affect the checkboxes in the dialog:
for (int i=0; i<checked.length; i++)
if (checked[i])
lv.setItemChecked(i, true);
else
lv.setItemChecked(i, false);
}

最佳答案

好吧,我终于找到了解决这个问题的方法,也许它可以帮助任何人:

我发现(由于内部缓存机制)最好不要在 Activity 中调用 showDialog() 并使用 onCreateDialog() 创建对话框,如果对话框的内容是动态修改的。

相反,我创建了一个扩展 AlertDialog.Builder 的类。我实现了一个类似“showCustomDialog()”的方法,我调用了 .setTitle...、.setMultiChoiceItems(),最后是 show()。我可以在我的 Activity 中使用我的自定义 AlertDialog.Builder 的这种方法,一切都按预期工作;)

关于android - 无法在 onPrepareDialog 中设置复选框状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4111863/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com