简体   繁体   English

如何在进度对话框中设置取消按钮?

[英]How to set a cancel button in Progress Dialog?

I want to set a cancel button in my ProgressDialog .我想在我的ProgressDialog中设置一个取消按钮。 Below is my code:下面是我的代码:

myDialog = new ProgressDialog(BaseScreen.this);
myDialog.setMessage("Loading...");
myDialog.setCancelable(false);
myDialog.show();

I want to set a button with an onClickListener on this ProgressDialog .我想在此ProgressDialog上设置一个带有onClickListener的按钮。 I tried with this code:我试过这段代码:

myDialog.setButton("Cancel", new OnClickListener() {        
    @Override
    public void onClick(DialogInterface dialog, int which) {
        // TODO Auto-generated method stub          
        myDialog.dismiss();
    }
});

But it isn't working.但它不起作用。 I tried other similar listeners also, but still no success.我也尝试了其他类似的听众,但仍然没有成功。 How can I solve this problem?我怎么解决这个问题?

The setButton method you are using is deprecated (although it should still work).您正在使用的setButton方法已被弃用(尽管它应该仍然有效)。 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();

Make sure you call myDialog.setButton before calling myDialog.show();确保在调用myDialog.setButton myDialog.show();
Also you can use myDialog.setButton("Cancel", (DialogInterface.OnClickListener) null);您也可以使用myDialog.setButton("Cancel", (DialogInterface.OnClickListener) null); if you only need to close the dialog on button click.如果您只需要在按钮单击时关闭对话框。

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");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM