【问题标题】:Android : Alert Dialog with Multi ChoiceAndroid:带有多选的警报对话框
【发布时间】:2017-10-18 22:53:33
【问题描述】:

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

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

      }
    });        

【问题讨论】:

  • Lucifer:我尝试将 listview 项的 Clickable 和 Enable 属性设置为 false,但我无法正确禁用警报对话框中的其余行,它正在禁用列表中的所有行警报对话框。
  • @And_dev:请将您现有的代码添加到问题中。
  • @rIHaN JiTHiN:添加了我的代码 sn-p。

标签: android android-alertdialog


【解决方案1】:

您的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 项,如果“无”,它会将每个元素设置为启用项目没有被检查,如果没有项目被检查则被禁用。

最后,我要指出的是,这不是多选对话框的常见行为。用户会对“无”选项的行为感到惊讶,因为它与其他所有选项都不同。没有“无”选项会更常见:如果用户不检查任何其他选项,则表示没有。如果您确实需要“无”选项,以区分用户明确选择“无”和不回答之间的区别,请考虑使用带有单独“无”按钮或复选框组之外的单选按钮的自定义布局,这样用户就可以知道它的行为会有所不同。

【讨论】:

  • 感谢您的回答,这正是我所尝试的,但它正在禁用位置 '0' 并将位置 1 保留为启用状态。
  • 可能是AlertDialog 在顶部插入了一个额外的项目或其他东西,因此“无”项目实际上不在位置 0,即使它是您通过的第一个项目。在调试器中检查单击“无”项时传递的 which 的值。
  • 如果我使用这个属性 alertDialogList.getCheckedItemPosition() 我总是收到 -1 作为点击任何项目的位置。
  • 当然可以。 The documentation for getCheckedItemPosition() says "The result is only valid if the choice mode has been set to CHOICE_MODE_SINGLE."这就是我建议的代码不使用它的原因。
  • 只有当我将位置值作为 2 开始时,它的值才为 0,它将禁用从 0 到位置 -2 的项目。
【解决方案2】:
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();

【讨论】:

    【解决方案3】:

    是的,这是真的

                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 是什么?我想不通?我正在尝试显示带有多选复选框列表的警报对话框,但是当单击列表中的“无”选项时,所有行的其余部分都应该被禁用
    猜你喜欢
    • 2021-11-10
    • 1970-01-01
    • 2015-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-03
    • 2012-08-29
    相关资源
    最近更新 更多