Android:带有多选项的警报对话框

4

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

    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);
         }

      }
    });        

路西法:我尝试通过将ListView项的Clickable和Enable属性设置为false来禁用它,但我无法正确地禁用警报对话框中其余的行,它会禁用警报对话框中列表中的所有行。 - And_dev
@And_dev:请将您现有的代码添加到问题中。 - JiTHiN
@rIHaN JiTHiN:添加了我的代码片段。 - And_dev
3个回答

4
您的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);
     }

你可以从被点击的元素开始,禁用它以及它之后的所有子元素,直到列表末尾。只有在被点击元素之前的子元素不会被禁用。第二个问题是你的禁用代码将运行在任何被点击的项上,而不仅仅是“none”项。尝试使用以下代码。我使用which来识别是否按下了特殊的“none”项。
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”项目时禁用它们。
最后,我要注意的是,这不是多项选择对话框的通常行为。用户会惊讶于“none”选项的行为,因为它与其他所有选项都不同。更常见的是没有“none”选项:如果用户没有选中任何其他选项,则表示没有选择。如果您确实需要“none”选项,请考虑使用自定义布局,并在复选框组之外添加单独的“none”按钮或单选按钮,以便用户可以知道它的行为不同。

谢谢您的回答,这正是我尝试过的,但它禁用了位置“0”,而将位置-1保留为启用状态。 - And_dev
可能是 AlertDialog 在顶部插入了额外的项目,因此“无”项目实际上并不在位置0,即使它是您传递的第一个项目。在调试器中检查当您单击“无”项目时传递给 which 的值。 - Dan Hulme
如果我使用alertDialogList.getCheckedItemPosition()这个属性,无论点击哪个选项,我总是收到-1作为位置。 - And_dev
1
当然了。getCheckedItemPosition() 的文档说:“仅当选择模式设置为 CHOICE_MODE_SINGLE 时,结果才有效。”这就是为什么我建议的代码没有使用它的原因。 - Dan Hulme
只有当我将位置值设为2时,其值才为0,这会禁用从0到位置-2的项目。 - And_dev
显示剩余4条评论

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

-1

是的,它是真实的

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

那个textEntryView是什么?我不太明白。我正在尝试显示一个带有多选框列表的警告对话框,但当列表中的“无”选项被点击时,其余所有行都应该被禁用。 - And_dev

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接