android - 在 Android 中选择复选框之前的 AlertDialog

标签 android checkbox android-alertdialog confirmation

我有一个带有复选框的对话框。在选择此复选框之前,我想询问用户,他一定要选择这些复选框。 (我的意思是,我尝试选择复选框,然后在此 AlertDialog 出现之前询问我是否确定)

代码如下:

CheckBox cb = (CheckBox) v.findViewById(R.id.favouriteChkBox);

            if (cb.isChecked()) {
                AlertDialog.Builder builder = new AlertDialog.Builder(
                        context);
                builder.setTitle("Do you want to add this to favourite?");

                builder.setPositiveButton("yes",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int id) {
                                itemChecked.set(position, true);
                            }
                        });
                builder.setNegativeButton("no",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int id) {
                                dialog.cancel();
                            }
                        });
                builder.create().show();

            }

但这不起作用。显示警报对话框,无论我单击"is"还是“否”,复选框都会被选中。

你能告诉我为什么这不起作用吗? 谢谢!

最佳答案

    final CheckBox cb = (CheckBox) findViewById(R.id.favouriteChkBox);

    cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (cb.isChecked()) {
                AlertDialog.Builder builder = new AlertDialog.Builder(
                        TestGitActivity.this);
                builder.setTitle("Do you want to add this to favourite?");

                builder.setPositiveButton("yes",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int id) {
                                cb.setChecked(true);
                            }
                        });
                builder.setNegativeButton("no",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int id) {
                                cb.setChecked(false);
                            }
                        });
                builder.create().show();

            }

        }
    });

关于android - 在 Android 中选择复选框之前的 AlertDialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15318784/

相关文章:

android - 升级到 android studio 花栗鼠后,应用程序未安装在模拟器中

android - parseInt 崩溃的安卓应用程序

Delphi FMX TCheckbox/TRadiobutton 自动调整大小

javascript - AngularJS限制要检查的复选框和要输入的文本框的数量

javascript - 使用 jQuery 实现双向级联复选框

android - 如何在editText框的右下角显示editText字符限制??下面的例子

android - 动态更改源后,Xamarin Forms ImageButton 变小

android - 为什么我开发的 Android 应用程序没有通过 Google Play 自动更新?

android - "android.view.WindowManager$BadTokenException: Unable to add window"上 buider.show()

android - Android 对话框的最佳实践 - 如何避免应用程序因关闭对话框而崩溃