设置复选框列表对话框的复选框

发布于 2024-09-29 17:39:55 字数 1615 浏览 3 评论 0原文

我有一个显示复选框列表的对话框。我想设置每次显示对话框时选中的不同框。但这仅在第一次有效。我希望每次显示对话框时它都有效!如果有人可以提供帮助,那就太好了...

这是我的代码:

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {

            case CHECKBOX_LIST_DIALOG:

                final CharSequence[] weeks = new CharSequence[53];

                for (int i=0; i<=52; i++) {
                    weeks[i] = String.valueOf(i+1);
                }

                return new AlertDialog.Builder(this).setTitle(
                        R.string.txt_week_checkbox_list).setMultiChoiceItems(
                        weeks, getCheckedBoxes(),
                        new DialogInterface.OnMultiChoiceClickListener() {
                            public void onClick(DialogInterface dialog, int whichButton, boolean isChecked) {
                                checked[whichButton] = isChecked;
                            }
                        }).setPositiveButton("OK",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int whichButton) {
                                EditText editText = (EditText) findViewById(R.id.edittext_weeks);
                                editText.setText(generateString());
                            }
                        }).setNegativeButton("Cancel",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int whichButton) {
                            }
                        }).create();
}

I've got a dialog which shows a list of checkBoxes. I'd like to set different boxes checked each time the dialog is showed. But that only works the first time.. I want it work every time the dialog is showed! It would be great if anyone can help...

This is my code:

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {

            case CHECKBOX_LIST_DIALOG:

                final CharSequence[] weeks = new CharSequence[53];

                for (int i=0; i<=52; i++) {
                    weeks[i] = String.valueOf(i+1);
                }

                return new AlertDialog.Builder(this).setTitle(
                        R.string.txt_week_checkbox_list).setMultiChoiceItems(
                        weeks, getCheckedBoxes(),
                        new DialogInterface.OnMultiChoiceClickListener() {
                            public void onClick(DialogInterface dialog, int whichButton, boolean isChecked) {
                                checked[whichButton] = isChecked;
                            }
                        }).setPositiveButton("OK",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int whichButton) {
                                EditText editText = (EditText) findViewById(R.id.edittext_weeks);
                                editText.setText(generateString());
                            }
                        }).setNegativeButton("Cancel",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int whichButton) {
                            }
                        }).create();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦里南柯 2024-10-06 17:39:55

通过 onCreateDialog() 创建的托管对话框会被缓存。您需要重写onPrepareDialog(),以便您在下次显示对话框时获得控制权。您将传递 Dialog 对象。将其转换为 AlertDialog,调用 getListView(),并使用 setItemChecked() 打开或关闭每个复选框。

Managed dialogs created via onCreateDialog() are cached. You will need to override onPrepareDialog(), so you get control when the dialog is next shown. You are passed the Dialog object. Cast that to an AlertDialog, call getListView(), and use setItemChecked() to toggle each checkbox on or off.

美人骨 2024-10-06 17:39:55

伟大的!就这样搞定了,谢谢!!这正是我一直在寻找的:-)
这是我为使其像您所解释的那样工作所做的:

@Override
protected void onPrepareDialog(int id, Dialog dialog) {
    ListView lv = ((AlertDialog) dialog).getListView();
    boolean[] checked = myDialog.getCheckedBoxes();
    for (int i=0; i<checked.length; i++)
        if (checked[i])
            lv.setItemChecked(i, true);
}

Great! That did it, thanks!! That was exactly what I was looking for :-)
Here's what I did to make it work like you explained:

@Override
protected void onPrepareDialog(int id, Dialog dialog) {
    ListView lv = ((AlertDialog) dialog).getListView();
    boolean[] checked = myDialog.getCheckedBoxes();
    for (int i=0; i<checked.length; i++)
        if (checked[i])
            lv.setItemChecked(i, true);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文