复选框对话框以编程方式选择

时间:2014-07-02 13:59:02

标签: android dialog

我想弄清楚如何在我设置的复选框对话框代码中以编程方式选中我的复选框。

我如何能够以编程方式从我的onClick方法切换下面的复选框。

如果到目前为止,任何人都可以查看我的代码,并给我一些指导,那就太棒了。

代码:

            AlertDialog dialog;

        final CharSequence[] items = { "All listings", "Buy it now",
                "Auction", "Nearest first" };

        final ArrayList<Integer> seletedItems = new ArrayList<Integer>();
        final SharedPreferences shfObject = getActivity()
                .getSharedPreferences("NAME", Context.MODE_PRIVATE);
        final SharedPreferences.Editor shfEditorObject = shfObject.edit();
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle("Filter");
        builder.setMultiChoiceItems(items, null, new DialogInterface.OnMultiChoiceClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog,
                            final int indexSelected, boolean isChecked) {
                        if (isChecked) {





                        }
                    }
                })
                // Set the action buttons
                .setPositiveButton("OK",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog,
                                    int id) {





                            }
                        })
                .setNegativeButton("Cancel",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog,
                                    int id) {
                                // Your code when user clicked on Cancel

                            }
                        });

        dialog = builder.create();// AlertDialog dialog; create like this
                                    // outside onClick
        dialog.show();

1 个答案:

答案 0 :(得分:0)

在第二个参数中,您必须通过null而不是new boolean[]{false, true, false, true, false}(取决于您的复选框)。它将选择您想要的复选框。

Referenced Link

How do i obtain the setMultiChoiceItems items from my AlertDialog?