【问题标题】:Can't set checkbox state in onPrepareDialog无法在 onPrepareDialog 中设置复选框状态
【发布时间】:2010-11-06 04:17:04
【问题描述】:

我有一个显示复选框列表的对话框。每次打开时应检查哪些框的信息来自编辑文本。我搜索了一种不允许缓存对话框的方法,但找不到如何做到这一点。现在我覆盖 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);
    }

【问题讨论】:

    标签: android checkbox dialog checkboxlist prepare


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-14
      • 2013-12-16
      • 2016-02-17
      • 2020-04-17
      相关资源
      最近更新 更多