How to set a cancel button in Progress Dialog?

前端 未结 3 617
温柔的废话
温柔的废话 2020-12-13 08:18

I want to set a cancel button in my ProgressDialog. Below is my code:

myDialog = new ProgressDialog(BaseScreen.this);
myDialog.setMessage(\"Load         


        
相关标签:
3条回答
  • 2020-12-13 08:46

    Make sure you call myDialog.setButton before calling myDialog.show();
    Also you can use myDialog.setButton("Cancel", (DialogInterface.OnClickListener) null); if you only need to close the dialog on button click.

    0 讨论(0)
  • 2020-12-13 08:46

    check this

    private void createCancelProgressDialog(String title, String message, String buttonText)
    {
        cancelDialog = new ProgressDialog(this);
        cancelDialog.setTitle(title);
        cancelDialog.setMessage(message);
        cancelDialog.setButton(buttonText, new DialogInterface.OnClickListener() 
        {
            public void onClick(DialogInterface dialog, int which) 
            {
                // Use either finish() or return() to either close the activity or just the dialog
                return;
            }
        });
        cancelDialog.show();
    }
    

    then just use a simple call method from elsewhere in your activity

    createCancelProgressDialog("Loading", "Please wait while activity is loading", "Cancel");
    
    0 讨论(0)
  • 2020-12-13 08:49

    The setButton method you are using is deprecated (although it should still work). Also, you might want to add the button before showing the dialog. Try:

    myDialog = new ProgressDialog(BaseScreen.this);
    myDialog.setMessage("Loading...");
    myDialog.setCancelable(false);
    myDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            myDialog.dismiss();//dismiss dialog
        }
    });
    myDialog.show();
    
    0 讨论(0)
提交回复
热议问题