安卓:警告对话框中的多选 [英] Android : Alert Dialog with Multi Choice

查看:107
本文介绍了安卓:警告对话框中的多选的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能显示警告对话框,在列表中选择多有残疾项目(行)?
在列表中选中无选项列表中的所有选项应该选择除无禁用得到,如果我取消选择无,需要再次启用所有项目?

  AlertDialog.Builder dialogBu​​ilder =新AlertDialog.Builder(背景);
    dialogBu​​ilder.setMultiChoiceItems(optionsList,selectionState,新
                                       DialogInterface.OnMultiChoiceListener()
    {      @覆盖
      公共无效的onClick(DialogInterface对话,诠释它,布尔器isChecked){      最后AlertDialog alertDialog =(AlertDialog)对话框;
      最终的ListView alertDialogList = alertDialog.getListView();        //这里如何在列表中禁用的项目被点击时无
        //无选项是optionsList字符串数组中的一         //一个循环,以禁止其它所有项目比点击1
         对于(INT位置= alertDialogList.getCheckedItemPosition();位置<
                                alertDialogList.getChildCount;位置++)
         {
                alertDialogList.getChildAt(位置).setEnabled(假);
         }      }
    });


解决方案

OnMultiChoiceClickListener 几乎是在那里。它只是有两个问题:第一,你的循环没有遍历除单击个都孩子

  //循环来禁用比点击一个其他所有项目
     对于(INT位置= alertDialogList.getCheckedItemPosition();位置<
                            alertDialogList.getChildCount;位置++)
     {
            alertDialogList.getChildAt(位置).setEnabled(假);
     }

您从单击之一启动,并禁用之一,那么之后所有的孩子吧,直到列表的末尾。只有那些的孩子严格之前的点击的一种呢没有得到禁止。第二个问题是,你禁用code会为被点击,而不仅仅是无项目的任何项目上运行。尝试这样的事情吧。我使用来识别特殊的无项目是否已经pressed。

 私有静态最终诠释specialItem = ...;
公共无效的onClick(DialogInterface对话,诠释它,布尔器isChecked){
    如果(这== singleItem){//只有当他们点击'无'
        最后AlertDialog alertDialog =(AlertDialog)对话框;
        最终的ListView alertDialogList = alertDialog.getListView();        对于(INT位置= 0;&位置LT; alertDialogList.getChildCount();位置++)
        {
            如果(位置!=其中){
                alertDialogList.getChildAt(位置).setEnabled(器isChecked!);
            }
        }
    }
}

请注意,我没有做任何事情,如果不是0.我的循环开始1,以避免项目0,并将其设置的每一个元素,如果无项目是启用的未签的,和残疾人,如果没有项目的的检查。

最后了,我就注意到,这不是多选择对话框中的通常行为。用户会惊奇关于无选项的行为,因为它是从一切不同。这将是更往常一样没有一个无选项:如果用户不检查任何其他选项,这意味着没有。如果你真的需要一个'无'选项,告诉用户之间的差异显式地采摘无,只是不回答,可以考虑使用自定义布局与一个单独的无按钮或单选按钮,这组复选框之外,这样用户可以告诉它会表现不同。

Is it possible to show Alert Dialog with Multi Choice with disabled items(Rows) in the list? By checking "None" Option in the list all options in the list should get disabled except option "None", if i uncheck option "None" need to enable all the items once again?

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

      }
    });        

解决方案

Your OnMultiChoiceClickListener is nearly there. It just has two problems: first, your for loop isn't iterating over all the children except the clicked one.

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

You start from the clicked one, and disable that one, then all the children after it, until the end of the list. Only children that are strictly before the clicked one don't get disabled. The second problem is that your disabling code will run for any item that's clicked, not just the 'none' item. Try something like this instead. I'm using which to identify whether the special 'none' item has been pressed.

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

Notice that I don't do anything at all if which isn't 0. My for loop starts from 1 in order to avoid item 0, and it sets every element to be enabled if the 'none' item was not checked, and disabled if the none item was checked.

Last off, I'll just note that this isn't the usual behaviour for multi-choice dialogs. The user will be surprised about the behaviour of the 'none' option, because it's different from everything else. It would be more usual to not have a 'none' option: if the user doesn't check any other option, that means none. If you really do need a 'none' option, to tell the difference between the user explicitly picking 'none' and just not answering, consider using a custom layout with a separate 'none' button or radio button that's outside the group of checkboxes, so the user can tell it will behave differently.

这篇关于安卓:警告对话框中的多选的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆