Android后退按钮和进度对话框

时间:2011-03-09 23:29:06

标签: android button dialog progress back

我有一个 AsyncTask ,它在工作时显示progressDialog(它从 doInBackground 中调用 runOnUiThread 来显示进度对话框)。

虽然它正在运行但我想允许使用后退按钮来取消操作;其他人遇到过这个问题:BACK Button is not working ,while progressDialog is running

因为什么原因我不能回复那个帖子,因此不得不开始另一个?! (另一天的另一个问题)

我和Sandy有同样的想法,但是当 progressDialog 显示时,这个代码从未被调用过,为什么会这样?我已经在我的主要活动类中实现了它, progressDialog 是否暂时将我的课程重点放在我的课堂上?

9 个答案:

答案 0 :(得分:44)

首先,您应该从OnPreExecute显示对话框,将其隐藏在OnPostExecute中,并在必要时通过发布进度进行修改。 (见here

现在回答您的问题:ProgressDialog.show()可以将OnCancelListener作为参数。您应该在进度对话框实例上提供一个调用cancel()的文件。

示例:

    @Override
    protected void onPreExecute(){
        _progressDialog = ProgressDialog.show(
                YourActivity.this,
                "Title",
                "Message",
                true,
                true,
                new DialogInterface.OnCancelListener(){
                    @Override
                    public void onCancel(DialogInterface dialog) {
                        YourTask.this.cancel(true);
                        finish();
                    }
                }
        );
    }

其中_progressDialogProgressDialog的{​​{1}}成员。

  

此类在API级别26中已弃用.ProgressDialog是一种模式   对话框,阻止用户与应用程序交互。代替   使用这个类,你应该使用像这样的进度指示器   ProgressBar,可以嵌入到您应用的UI中。或者,   您可以使用通知来通知用户任务的进度。 LINK

答案 1 :(得分:14)

这可以通过以下代码片段来实现:

progress.setCancelable(true);
progress.setCanceledOnTouchOutside(false);

进度是ProgressDialog对象......

这将启用后退按钮关闭对话框,但阻止任何触摸输入执行此操作...

答案 2 :(得分:13)

好吧,我有同样的问题。对我有用的最简单的方法是使用progressDialog.setCancelable(true) ..这声明对话框是否可以通过按后退键来取消。试试看,让我知道它是否适合你。祝你好运

答案 3 :(得分:8)

我刚刚找到了解决这个问题的完美而简单的解决方案。 ProgressDialog中有一个方法可以设置KeyListener。

progressDialog.setOnKeyListener(new OnKeyListener() {
    @Override
    public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
        if(keyCode==KeyEvent.KEYCODE_BACK && !event.isCanceled()) {
            if(progressDialog.isShowing()) {
                //your logic here for back button pressed event
            } 
            return true;
        }
        return false;
    }
});

setCancelable(false)正常运行。我也在检查event.isCanceled(),因为没有我,我得到两个事件。我已经使用和不使用硬件密钥在Lollipop设备上进行了测试。

答案 4 :(得分:5)

像取消一样处理后退按钮不是正确的方法 当用户触摸对话框外部的屏幕时,也会发生取消。 你想区分这两个动作,不是吗?

正确的方法是扩展ProgressDialog类并覆盖onBackPressed方法。

private class SubProgressDialog extends ProgressDialog {
    public SubProgressDialog(Context context) {
        super(context);
    }
    @Override
    public void onBackPressed() {
        /** dismiss the progress bar and clean up here **/
    }
}


public void displayProgressBar(){
    progressBar = new SubProgressDialog(this);
    progressBar.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    progressBar.setCancelable(false);
    progressBar.setMessage(getString(R.string.authorizing));        
    progressBar.show();   
    new Thread(new Runnable() {
      public void run() {
      }
       }).start();     

}

注意setCancelable(false),再次强调后退按钮与简单取消不同 此外,这将有效地忽略用户的任何其他触摸输入。

答案 5 :(得分:3)

这是我的解决方案。 PD问题仅在应用程序退出后退按钮时发生(通常当用户重复按下它时,并且有时(并非所有时间)当应用程序已被破坏时调用PD。解雇PD onDestroy不会&# 39; t由于某种原因而工作,当我尝试过它时,每次按下按钮都会关闭应用程序虽然canGoBack()设置为true。相反,我在goBack上取消了PD,这是导致碰撞的事件,我在finish()之前就开始了。如果应用程序退出goBack,那么首先没有问题。

BTW,'差异'旨在让用户通过快速双击(两次点击之间400 MS)退出应用程序,而不是从一页到另一页返回。

希望它有所帮助...

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (event.getAction() == KeyEvent.ACTION_DOWN) {
        switch (keyCode) {
        case KeyEvent.KEYCODE_BACK:
            difference = System.currentTimeMillis() - startTime;
            if (wView.canGoBack() && difference > 400) {
                wView.goBack();
            } else {
                dismissProgressDialog();
                finish();
            }
            startTime = System.currentTimeMillis();
            return true;
        }
    }
    return super.onKeyDown(keyCode, event);
}


private void dismissProgressDialog() {
   if (progressDialog != null && progressDialog.isShowing()) {
        progressDialog.dismiss();
   }
}

答案 6 :(得分:2)

javano ...我使用标准技术asyncTask和ProgressDialog测试了你的场景。在我的测试中,当progressDialog显示并且我回击时,progressdialog被解除并且后台线程继续运行。我不知道为什么你需要调用runOnUiThread。

SO:

在onPreExecute中显示progressDialog 将长时间运行的任务放在doInBackground中 在onPostExecute中关闭progressDialog 将输入参数传递给长时间运行的任务,如:

new MyAsynch().execute(password,inString);

取消()onPause中的asynchTask和progressDialog

protected void onPause() {
    super.onPause();
    if (asynch != null) {asynch.cancel(true);}
    if (progress != null){progress.cancel();}
}

JAL

我有一些代码here

答案 7 :(得分:1)

非常简单,只需复制下面的代码并粘贴在Async任务中。

ProgressDialog对话框;

@Override
protected void onPreExecute() {

    dialog = new ProgressDialog(MainActivity.this) {
        @Override
        public void onBackPressed() {
            dialog.cancel();
            dialog.dismiss();
        }
    };
    // dialog.setTitle("file is..");//its optional If u want set title in progress
    // bar
    dialog.setMessage("Loading file....");
    dialog.setCancelable(false);

    dialog.show();

}

答案 8 :(得分:0)

请按照此步骤显示取消按钮,只显示async,点击取消按钮

即可完成
protected void onPreExecute() {


        dialogx.setMessage("Loading... Please Wait...");
        dialogx.setCancelable(false);
        dialogx.setButton(DialogInterface.BUTTON_NEGATIVE,
                "Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                dialogx.dismiss();
                GetDataTask.this.cancel(true);
                finish();
            }
        });
        dialogx.show();


    }