Android ProgressDialog can't add Cancel button

风流意气都作罢 提交于 2019-12-13 18:52:25

问题


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:


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


来源:https://stackoverflow.com/questions/15293146/android-progressdialog-cant-add-cancel-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!