问题
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