Android 后退按钮和进度对话框

Android Back Button and Progress Dialog(Android 后退按钮和进度对话框)
本文介绍了Android 后退按钮和进度对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

I have an AsyncTask that shows a progressDialog whilst working (it calls runOnUiThread from within doInBackground to show the progress dialog).

在运行时,我想允许使用后退按钮取消操作;其他人遇到了这个问题:返回按钮不起作用,而progressDialog 正在运行

Whilst its running I want to allow the use of the back button to cancel the operation; someone else has had this problem: BACK Button is not working ,while progressDialog is running

由于什么原因我无法回复该主题,因此不得不开始另一个?!(另一天的另一个问题)

For what ever reason I can't reply to that thread, hence having to start another?! (Another question for another day)

我和 Sandy 有同样的想法,但是当 progressDialog 显示时,这段代码从未被调用,这是为什么呢?我已经在我的主要活动类中实现了它,progressDialog 是否暂时将前台焦点从我的类中移开?

I had the same idea as Sandy but this code is never called whilst the progressDialog is showing, why is this? I have implemented it inside my main activity class, does the progressDialog take the foreground focus away from my class temporarily?

推荐答案

首先,您应该从 OnPreExecute 显示您的对话框,将其隐藏在 OnPostExecute 中,并且 - 如有必要- 通过发布进度修改它.(参见这里)

First, you should show your dialog from OnPreExecute, hide it in OnPostExecute, and - if necessary - modify it by publishing progress. (see here)

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

Now to your question: ProgressDialog.show() can take a OnCancelListener as an argument. You should provide one that calls cancel() on the progress dialog instance.

示例:

    @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();
                    }
                }
        );
    }

其中 _progressDialogYourTaskProgressDialog 成员.

where _progressDialog is a ProgressDialog member of YourTask.

此类在 API 级别 26 中已弃用.ProgressDialog 是一种模式对话框,它会阻止用户与应用程序交互.反而使用此类,您应该使用进度指示器,例如ProgressBar,可以嵌入到应用程序的 UI 中.或者,您可以使用通知来通知用户任务的进度.链接

This class was deprecated in API level 26. ProgressDialog is a modal dialog, which prevents the user from interacting with the app. Instead of using this class, you should use a progress indicator like ProgressBar, which can be embedded in your app's UI. Alternatively, you can use a notification to inform the user of the task's progress. LINK

这篇关于Android 后退按钮和进度对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How To Create a Rotating Wheel Control?(如何创建转轮控件?)
How to avoid restarting activity when orientation changes on Android(如何在 Android 上的方向更改时避免重新启动活动)
How can you rotate text for UIButton and UILabel in Swift?(如何在 Swift 中旋转 UIButton 和 UILabel 的文本?)
How can you rotate text for UIButton and UILabel in Objective-C?(如何在 Objective-C 中旋转 UIButton 和 UILabel 的文本?)
Screen orientation lock(屏幕方向锁定)
Strange behavior with android orientation sensor(android方向传感器的奇怪行为)