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 can I use CClistview in COCOS2d Android?(如何在 COCOS2d Android 中使用 CClistview?)
cocos2d-android: how to display score(cocos2d-android:如何显示分数)
cocos2d 3.x CCButton set the sprite frame image distorted and callback/block not working(cocos2d 3.x CCButton 设置精灵帧图像扭曲和回调/块不起作用)
Sqlite database not copied from asset folder Android(Sqlite 数据库未从资产文件夹 Android 复制)
SQLite Database Copy Appears Corrupted When Generated by Device and not Emulator(SQLite 数据库副本在由设备而不是模拟器生成时出现损坏)
Android file copy(安卓文件拷贝)