Android:具有多选的警报对话框

时间:2012-10-18 02:15:37

标签: android alertdialog

是否可以在列表中显示具有禁用项目(行)的多项选择的警报对话框? 通过选中列表中的“无”选项,除了选项“无”外,列表中的所有选项都将被禁用,如果我取消选中“无”选项,则需要再次启用所有项目吗?

    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);
    dialogBuilder.setMultiChoiceItems(optionsList,selectionState,new
                                       DialogInterface.OnMultiChoiceListener()
    {

      @Override
      public void onClick(DialogInterface dialog,int which, boolean isChecked){

      final AlertDialog alertDialog = (AlertDialog) dialog;
      final ListView alertDialogList = alertDialog.getListView();

        // Here how to make the items in the list as disabled when None is clicked
        // None OPtion is one among  in optionsList string array

         // A loop to disable all items other than clicked one 
         for (int position = alertDialogList.getCheckedItemPosition(); position<
                                alertDialogList.getChildCount; position++)
         {
                alertDialogList.getChildAt(position).setEnabled(false);
         }

      }
    });        

3 个答案:

答案 0 :(得分:3)

你的OnMultiChoiceClickListener几乎就在那里。它只有两个问题:首先,你的for循环不会迭代除被点击的所有子项之外的所有子项。

     // A loop to disable all items other than clicked one 
     for (int position = alertDialogList.getCheckedItemPosition(); position<
                            alertDialogList.getChildCount; position++)
     {
            alertDialogList.getChildAt(position).setEnabled(false);
     }

从单击的那个开始,然后禁用那个,然后禁用它之后的所有子项,直到列表的末尾。只有严格所点击的儿童才能被禁用。第二个问题是,您的禁用代码将针对任何被点击的项目运行,而不仅仅是“无”项目。尝试这样的事情。我正在使用which来确定是否已按下特殊的“无”项目。

private static final int specialItem = ...;
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
    if (which == singleItem) { // only if they clicked 'none'
        final AlertDialog alertDialog = (AlertDialog) dialog;
        final ListView alertDialogList = alertDialog.getListView();

        for (int position = 0; position < alertDialogList.getChildCount(); position++)
        {
            if (position != which) {
                alertDialogList.getChildAt(position).setEnabled(!isChecked);
            }
        }
    }
}

请注意,如果which不为0,我根本不做任何事情。我的for循环从1开始以避免项0,并且它设置每个元素都要启用“无”项目已选中,如果 未选中则会被禁用。

最后,我会注意到这不是多选对话框的通常行为。用户会对'none'选项的行为感到惊讶,因为它与其他所有选项不同。更常见的是没有'none'选项:如果用户没有检查任何其他选项,那意味着没有。如果您确实需要“无”选项,要告诉用户明确选择“无”并且只是不回答,请考虑使用自定义布局,单独的“无”按钮或单选复选框外的单选按钮,所以用户可以告诉它会有不同的行为。

答案 1 :(得分:0)

是的,这是真的

            new AlertDialog.Builder(Main.this)
            .setIcon(R.drawable.icon)
            .setTitle("Title")
            .setView(textEntryView)
            .setPositiveButton("Save", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    //android.os.Debug.waitForDebugger();


                    /* User clicked OK so do some stuff */ 
                }
            })
            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                    /* User clicked cancel so do some stuff */
                }
            })
            .setNeutralButton("Delete", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                }
            })
            .create();

答案 2 :(得分:0)

AlertDialog alertDialog  = new AlertDialog.Builder(context).create();   
            alertDialog.setTitle("Warning!");
            alertDialog.setMessage("Confirm closing activity without succes?");
            alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "Yes", new DialogInterface.OnClickListener() {


                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    UpdateWebActivityState(ActivitiesEditActivity.this, serviceActivity.ActivityId,serviceActivity.WebActivityState , notes, sigBitmap);
                    isSuccessfullyClosed = false;
                    AlertDialog alert  = new AlertDialog.Builder(context).create(); 
                    alert.setTitle("Warning!");
                    alert.setMessage("Activity closed successfully");
                    alert.setButton(DialogInterface.BUTTON_POSITIVE, "Ok", new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dialog, int which) {
                            // TODO Auto-generated method stub

                            do what you want here
                            finish();                   
                        }

                    });

                    alert.show();

                }
            });

            alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "No", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which)
                {
                        return;
                }
                });

            alertDialog.show();