Android ProgressDialog can't add Cancel button

2019-08-05 10:58发布

I want to add a cancel button to my progress dialog but I can't compile the code. The IDE (eclipse) it's saying that there is an error in the code but I don't know what's wrong?

ProgressDialog ASYN_DIALOG = new ProgressDialog(getBaseContext());
ASYN_DIALOG.setMessage("Awaiting...");
ASYN_DIALOG.setButton("Cancel", new OnClickListener() {

    @Override
    public void onClick(DialogInterface dialog, int which) {
       Log.e("ANDR: ", "Cancel clicked !");     
    }
});

I'm using API lvl 10 (Android 2.3.3)

1条回答
Viruses.
2楼-- · 2019-08-05 11:59

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(this);
myDialog.setMessage("Loading...");
myDialog.setCancelable(false);
myDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        dialog.dismiss();
    }
});
myDialog.show();
查看更多
登录 后发表回答