2012-10-18 42 views
3

是否可以在列表中显示具有多项选择功能的警报对话框(禁用项目(行))? 通过选中列表中的“无”选项列表中的所有选项应该被禁用,除非选项“无”,如果我取消选中选项“无”需要再次启用所有项目?安卓系统:带多选项的警报对话框

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

你有试过什么吗? – Lucifer

+0

是的,可能的。 – JiTHiN

+0

Lucifer:我尝试了将Listview Item的Clickable和Enable属性设置为false,但我无法正确禁用警报对话框中的其余行,它将禁用警报对话框中列表中的所有行。 –

回答

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

什么是textEntryView?我无法得到这个想法?我试图显示多选的复选框列表的警报对话框,但当列表中的“无”选项被点击时,所有行的其余部分应该被禁用 –

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(); 
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开始,并将其设置,如果“无”启用每一个元素项目是不是检查,如果没有项目检查,则禁用。

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

+0

谢谢你的回答这是我完全尝试过,但它是禁用位置'0'并且将位置1保持为启用状态。 –

+0

AlertDialog可能会在顶部或某处插入一个额外的项目,以便'none'项目实际上不在0位置,即使这是您传递的第一个项目。检查调试器当你点击'无'项目时你通过了哪个'哪个'值。 –

+0

如果我使用此属性alertDialogList.getCheckedItemPosition()我总是会获得-1作为任何项目单击位置。 –