如何避免在用户触摸屏幕时关闭我的进度对话框?

2022-09-01 04:04:01

我正在开发一个Android项目,当用户下载文件时,它会显示一个进度对话框。

但是,当用户触摸屏幕时,进度对话框将被关闭,而无需等待100%。我已经尝试过使用这个:

 public boolean onTouchEvent(MotionEvent e) {
        return true;
    } 

但它不起作用。

我该如何避免这种情况?

更新 1:

似乎setCancelable(false)工作正常。非常感谢您的回答,但是当下载持久并且用户决定放弃它时,这将是不可能的,因为我已经停用了后面的keyCode:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

我该如何解决这个问题?


答案 1

dialog.setCancelable(false);

例:

        ProgressDialog dialog = new ProgressDialog(WiFiFinderActivity.this);
        dialog.setMessage("please wait...");
        dialog.show();
        dialog.setCancelable(false);
        dialog.setCanceledOnTouchOutside(false);

答案 2

您不想使用此覆盖函数。您刚刚设置

final Dialog dialog=new Dialog(dialogactivity.this);
dialog.setCancelable(false);
dialog.setCanceledOnTouchOutside(false);

推荐