【问题标题】:Android ProgressDialog can't add Cancel buttonAndroid ProgressDialog 无法添加取消按钮
【发布时间】:2013-02-23 22:44:12
【问题描述】:

我想在我的进度对话框中添加一个取消按钮,但我无法编译代码。 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 !");     
    }
});

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

【问题讨论】:

  • “说有错误”是什么意思?仔细检查您的导入并查看 OnClickListener 的导入是否是来自 DialogInterface 的替换 new OnClickListenernew DialogInterface.OnClickListener

标签: android eclipse button compilation


【解决方案1】:

您使用的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();

【讨论】:

  • 您不必关闭侦听器内的对话框,因为取消按钮会自动为您关闭对话框。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-10-14
  • 2015-12-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多