Android Dialog, keep dialog open when button is pressed

后端 未结 7 1312
故里飘歌
故里飘歌 2020-11-30 01:59

I would like to keep my dialog open when I press a button. At the moment it\'s closing.

AlertDialog.Builder builder = new AlertDialog.Builder(this);

builder         


        
相关标签:
7条回答
  • 2020-11-30 02:38

    Yes, you can. You basically need to:

    1. Create the dialog with DialogBuilder
    2. show() the dialog
    3. Find the buttons in the dialog shown and override their onClickListener

    So, create a listener class:

    class CustomListener implements View.OnClickListener {
      private final Dialog dialog;
    
      public CustomListener(Dialog dialog) {
        this.dialog = dialog;
      }
    
      @Override
      public void onClick(View v) {
    
        // Do whatever you want here
    
        // If you want to close the dialog, uncomment the line below
        //dialog.dismiss();
      }
    }
    

    Then when showing the dialog use:

    AlertDialog dialog = dialogBuilder.create();
    dialog.show();
    Button theButton = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
    theButton.setOnClickListener(new CustomListener(dialog));
    

    Remember, you need to show the dialog otherwise the button will not be findable. Also, be sure to change DialogInterface.BUTTON_POSITIVE to whatever value you used to add the button. Also note that when adding the buttons in the DialogBuilder you will need to provide onClickListeners - you can not add the custom listener in there, though - the dialog will still dismiss if you do not override the listeners after show() is called.

    0 讨论(0)
  • 2020-11-30 02:38

    You can get the Dialog returned from method "show()" alertBuidler.

    AlertDialog.Builder adb = new AlertDialog.Builder(YourActivity.this);
    //...code to add methods setPositive an setNegative buttons
    

    Call the "show()" method of "adb" and get Dialog

    final AlertDialog dialog = adb.show();
    

    So you can call any button of your dialog at any point of code in your activity:

    dialog.getButton(DialogInterface.BUTTON_POSITIVE).performClick();//or
    dialog.getButton(DialogInterface.BUTTON_NEGATIVE).performClick();//or
    dialog.getButton(DialogInterface.BUTTON_NEUTRAL).performClick();
    
    0 讨论(0)
  • 2020-11-30 02:44

    You will probably need to define your own layout and not use the "official" buttons; the behavior you're asking for is not typical of a dialog.

    0 讨论(0)
  • 2020-11-30 02:49

    I believe the answer by @Kamen is correct, here is an example of the same approach using an anonymous class instead so it is all in one stream of code:

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setMessage("Test for preventing dialog close");
    AlertDialog dialog = builder.create();
    dialog.show();
    //Overriding the handler immediately after show is probably a better approach than OnShowListener as described below
    dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener()
          {            
              @Override
              public void onClick(View v)
              {
                  Boolean wantToCloseDialog = false;
                  //Do stuff, possibly set wantToCloseDialog to true then...
                  if(wantToCloseDialog)
                      dismiss();
                  //else dialog stays open. Make sure you have an obvious way to close the dialog especially if you set cancellable to false.
              }
          });
    

    I wrote a more detailed write up to answer the same question here https://stackoverflow.com/a/15619098/579234 which also has examples for other dialogs like DialogFragment and DialogPreference.

    0 讨论(0)
  • 2020-11-30 02:50

    You do not need to create a custom class. You can register a View.OnClickListener for the AlertDialog. This listener will not dismiss the AlertDialog. The trick here is that you need to register the listener after the dialog has been shown, but it can neatly be done inside an OnShowListener. You can use an accessory boolean variable to check if this has already been done so that it will only be done once:

    /*
     * Prepare the alert with a Builder.
     */
    AlertDialog.Builder b = new AlertDialog.Builder(this);
    
    b.setNegativeButton("Button", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {}
    });
    this.alert = b.create();
    
    /*
     * Add an OnShowListener to change the OnClickListener on the
     * first time the alert is shown. Calling getButton() before
     * the alert is shown will return null. Then use a regular
     * View.OnClickListener for the button, which will not 
     * dismiss the AlertDialog after it has been called.
     */
    
    this.alertReady = false;
    alert.setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface dialog) {
            if (alertReady == false) {
                Button button = alert.getButton(DialogInterface.BUTTON_NEGATIVE);
                button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //do something
                    }
                });
                alertReady = true;
            }
        }
    });
    
    0 讨论(0)
  • 2020-11-30 02:54

    Thanks Sogger for your answer, but there is one change that we have to do here that is, before creating dialog we should set possitive button (and negative button if there is need) to AlertDialog as traditional way, thats it.

    Referenced By Sogger.

    Here is the sample example ...

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("Test for preventing dialog close");
            builder.setTitle("Test");
    
            builder.setPositiveButton("OK", new OnClickListener() {
    
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
    
                }
            });
        builder.setNegativeButton("Cancel", new OnClickListener() {
    
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
    
                }
            });
    
            final AlertDialog dialog = builder.create();
            dialog.show();
            //Overriding the handler immediately after show is probably a better approach than OnShowListener as described below
            dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener()
                  {            
                      @Override
                      public void onClick(View v)
                      {
                          Boolean wantToCloseDialog = false;
                          //Do stuff, possibly set wantToCloseDialog to true then...
                          if(wantToCloseDialog)
                              dialog.dismiss();
                          //else dialog stays open. Make sure you have an obvious way to close the dialog especially if you set cancellable to false.
                      }
                  });
    
            dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setOnClickListener(new View.OnClickListener()
              {            
                  @Override
                  public void onClick(View v)
                  {
                      Boolean wantToCloseDialog = true;
                      //Do stuff, possibly set wantToCloseDialog to true then...
                      if(wantToCloseDialog)
                          dialog.dismiss();
                      //else dialog stays open. Make sure you have an obvious way to close the dialog especially if you set cancellable to false.
                  }
              });
    
    0 讨论(0)
提交回复
热议问题