如何检查AlertDialog中的所有Checkbox项目

时间:2014-05-30 17:49:08

标签: android android-alertdialog

萨拉姆
如何检查Android AlertDialog

中的所有Checkbox项目(setMultiChoiceItems)
    AlertDialog.Builder builder = new AlertDialog.Builder(A);
    builder.setTitle(A.getString(R.string.which_number));
    builder.setIcon(R.drawable.ic_launcher_mini);
    builder.setMultiChoiceItems(line, null,
            new DialogInterface.OnMultiChoiceClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which,
                                    boolean isChecked) {
                }
            });

2 个答案:

答案 0 :(得分:9)

它可以帮到你吗?

AlertDialog.Builder builder = new AlertDialog.Builder(A);
    builder.setTitle(A.getString(R.string.which_number));
    builder.setIcon(R.drawable.ic_launcher_mini);
    builder.setMultiChoiceItems(line, null,
            new DialogInterface.OnMultiChoiceClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which,
                                    boolean isChecked) {
                    AlertDialog dialog = (AlertDialog) dialog;
                    ListView v = dialog.getListView();
                    int i = 0;
                    while(i < line.length) {
                        v.setItemChecked(i, check);
                        i++;
                    }
                }
            });

答案 1 :(得分:1)

答案非常简单

setMultiChoiceItems的第二个输入参数是默认的CheckBoxes状态

    int L = line.length;
    boolean[] b2 = new boolean[L];
    for(int i=0 ; i<L ; i++){
        b2[i]=true;
    }
    builder.setMultiChoiceItems(line, b2,
            new DialogInterface.OnMultiChoiceClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which,
                                    boolean isChecked) {