Android : Alert Dialog with Multi Choice

前端 未结 3 1085
囚心锁ツ
囚心锁ツ 2021-01-14 01:35

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 disab

相关标签:
3条回答
  • 2021-01-14 02:06
    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();
    
    0 讨论(0)
  • 2021-01-14 02:12

    Yes it's real

                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 讨论(0)
  • 2021-01-14 02:15

    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.

    0 讨论(0)
提交回复
热议问题