【问题标题】:Set checked boxes for a checkBox-list dialog设置复选框列表对话框的复选框
【发布时间】:2010-10-30 18:13:17
【问题描述】:

我有一个显示复选框列表的对话框。我想在每次显示对话框时设置不同的框。但这仅在第一次有效..我希望每次显示对话框时都能正常工作!如果有人能帮忙就太好了……

这是我的代码:

@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();
}

【问题讨论】:

    标签: android dialog checkbox checkboxlist


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      太棒了!做到了,谢谢!!这正是我想要的:-) 以下是我为使其按照您解释的方式工作所做的工作:

      @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);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-14
        • 2011-02-22
        • 2011-08-24
        • 2013-06-26
        • 1970-01-01
        • 2012-07-10
        相关资源
        最近更新 更多