android - android 中警报对话框自定义标题内的复选框处理

标签 android checkbox android-alertdialog

我的应用程序中有一个警报对话框,其中包含自定义标题和标题字段中的复选框,如下所示。 enter image description here

当选择全部复选框时,默认情况下应选中以下所有其他项目(多选列表),如下所示:

enter image description here

此外,如果未选中全部复选框,则之前的选择状态(即选择全部之前) )应勾选,其他项目应取消选择。我希望这一切在对话框聚焦时发生。

我尝试的是下面的代码。

@Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            int title = getArguments().getInt("title");


            View view = getActivity().getLayoutInflater().inflate(
                    R.layout.dialog_categories_title, null);

            cb = (CheckBox) view.findViewById(R.id.checkBox1);
            cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView,
                        boolean isChecked) {
                    if (isChecked) {
                        for (int i = 0; i < _selections.length; i++) {
                            _selections[i] = isChecked;

                        }
                    }

                }
            });

            return new AlertDialog.Builder(getActivity())
                    .setIcon(R.drawable.alert_dialog_icon)
                    .setTitle(title)
                    .setCustomTitle(view)

                    .setMultiChoiceItems(_categories, _selections,
                            new DialogSelectionClickHandler())

                    .setPositiveButton(R.string.alert_dialog_ok,
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int whichButton) {
                                    ((MainActivity) getActivity())
                                            .doPositiveClick();
                                }

                            }).create();
}

如果我运行此命令,当选中全部时,仅当我关闭并再次打开对话框时才会检查其他项目,但我希望在当前对话框处于焦点状态。我该怎么做?

最佳答案

    CheckBox repeatChkBx = (CheckBox) findViewById(R.id.repeat_checkbox);
    repeatChk2 = (CheckBox) findViewById(R.id.repeat_checkbox2);
            repeatChkBx.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                public void onCheckedChanged(CompoundButton buttonView,
                        boolean isChecked) {
                    if (isChecked) {
                        repeatChk2.setChecked(true);
                    }

                }
            });

关于android - android 中警报对话框自定义标题内的复选框处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13873053/

相关文章:

android-layout - Android 服务和 AlertDialog Activity

android - Windows 和 Android 应用程序是否需要 SSL?

android - 是什么导致了这个 NullpointerException?

grails - 获取表格中选定复选框的列表

javascript - 如何在 React Hooks 中切换复选框值?

android - AlertDialog 包装它的内容

Android:如何设置AlertDialog的宽高和AlertDialog样式的按钮?

java - 将位图资源存储在静态变量中

android - 添加航点以在位智 map 中导航

javascript - jQuery 文本框更改计算