How to prevent a dialog from closing when a button is clicked

后端 未结 18 2243
无人及你
无人及你 2020-11-21 23:59

I have a dialog with EditText for input. When I click the \"yes\" button on dialog, it will validate the input and then close the dialog. However, if the input

18条回答
  •  半阙折子戏
    2020-11-22 00:33

    For ProgressDialogs

    To prevent the dialog from being dismissed automatically you have to set the OnClickListener after the ProgressDialog is shown, like so:

    connectingDialog = new ProgressDialog(this);
    
    connectingDialog.setCancelable(false);
    connectingDialog.setCanceledOnTouchOutside(false);
    
    // Create the button but set the listener to a null object.
    connectingDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", 
            (DialogInterface.OnClickListener) null )
    
    // Show the dialog so we can then get the button from the view.
    connectingDialog.show();
    
    // Get the button from the view.
    Button dialogButton = connectingDialog.getButton( DialogInterface.BUTTON_NEGATIVE);
    
    // Set the onClickListener here, in the view.
    dialogButton.setOnClickListener( new View.OnClickListener() {
    
        @Override
        public void onClick ( View v ) {
    
            // Dialog will not get dismissed until you call dismiss() explicitly.
    
        }
    
    });
    

提交回复
热议问题