2

我有与复选框的对话框。在选择这个复选框之前,我想问一下用户,他一定会选择这些复选框。(我的意思是,我尝试选择复选框并在此 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();

            }

但这不起作用。如果我单击“是”或“否”,则会显示 AlertDialog 并且没有仪表,复选框被选中。

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

4

2 回答 2

2
    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();

            }

        }
    });
于 2013-03-10T04:23:09.017 回答
0

我怀疑这与这样一个事实有关,即如果用户选择NO ,则for 的状态CheckBox不会改变。试试这个代码:OnClickListenerNegativeButton

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);
            // ADD THIS TO YOUR CODE
            cb.setChecked(true);
        }
    });

    builder.setNegativeButton("no", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            // ADD THIS TO YOUR CODE
            .setChecked(false);
            dialog.dismiss(); // CHANGE THE .cancel() TO dismiss()
        }
    });

    builder.create().show();
}
于 2013-03-10T04:20:22.247 回答