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

      }
    });        
4

3 回答 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);
     }

你从被点击的那个开始,然后禁用那个,然后是它之后的所有孩子,直到列表的末尾。只有严格在点击之前的孩子不会被禁用。第二个问题是您的禁用代码将针对单击的任何项目运行,而不仅仅是“无”项目。试试这样的东西。我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,如果选中“无”项目,它会将每个元素设置为启用,如果没有检查任何项目

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

于 2012-10-18T10:22:27.590 回答
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();
于 2012-10-18T06:28:23.880 回答
-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();
于 2012-10-18T05:19:43.277 回答