Android ProgressDialog无法添加"取消"按钮

Zba*_*ian 1 eclipse android compilation button

我想在我的进度对话框中添加一个取消按钮,但我无法编译代码.IDE(eclipse)它说代码中有错误但我不知道什么是错的?

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 !");     
    }
});
Run Code Online (Sandbox Code Playgroud)

我正在使用API​​ lvl 10(Android 2.3.3)

DjH*_*orn 7

setButton您正在使用的方法已被弃用(尽管它应该仍然有效).此外,您可能希望在显示对话框之前添加按钮.尝试:

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();
Run Code Online (Sandbox Code Playgroud)