java 为什么 Android 的进度对话框中没有取消按钮?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1123971/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is there no cancel button in Android's progress dialogs?
提问by Jacques René Mesrine
I'm facing a head-scratching moment similar to what this person(from Jan 2008) experienced when I realized that there is no cancel button in Android's progress dialog or spinners. It is now July 2009 and I've just installed the cupcake version of Android. Has this thing changed? If not, are you adding a cancel button into the dialogs and how do you do it?
当我意识到 Android 的进度对话框或微调器中没有取消按钮时,我正面临着类似于此人(从 2008 年 1 月起)所经历的令人头疼的时刻。现在是 2009 年 7 月,我刚刚安装了纸杯蛋糕版的 Android。这件事有没有改变?如果没有,您是否在对话框中添加了取消按钮以及如何操作?
采纳答案by unwind
I'm no Android user or developer, but I think the answer given in the linked-to thread is pretty decent: there's a hardware"back" key on all Android devices. Users are supposed to know to press Back to back out of whatever activity they're currently in.
我不是 Android 用户或开发人员,但我认为链接线程中给出的答案相当不错:所有 Android 设备上都有一个硬件“后退”键。用户应该知道按 Back 退出他们当前正在进行的任何活动。
Thus, the UI designers feel it's unnecessary to include a GUI back/cancel button. This could be viewed as the UI application of the DRYprinciple; if there's already one way of doing something, that's enough.
因此,UI 设计人员认为没有必要包含 GUI 后退/取消按钮。这可以看作是DRY原则的 UI 应用;如果已经有一种做某事的方法,那就足够了。
回答by chich
not sure about the whole cancel button...i've heard reports of the onCancel() method not firing properly. my solution just consists of making a normal button on the dialog with a call to return whenever the button is pressed.
不确定整个取消按钮……我听说过 onCancel() 方法没有正确触发的报告。我的解决方案只是在对话框上制作一个普通按钮,并在按下按钮时调用返回。
private void createCancelProgressDialog(String title, String message, String buttonText)
{
cancelDialog = new ProgressDialog(this);
cancelDialog.setTitle(title);
cancelDialog.setMessage(message);
cancelDialog.setButton(buttonText, new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
// Use either finish() or return() to either close the activity or just the dialog
return;
}
});
cancelDialog.show();
}
then just use a simple call method from elsewhere in your activity
然后只需在您的活动中的其他地方使用一个简单的调用方法
createCancelProgressDialog("Loading", "Please wait while activity is loading", "Cancel");
rather simple solution, but it does the trick ;) also just to note that cancelDialog is an activity wipe variable, if you dont need to call it from elsewhere, then you should be able to get away with just limiting the scope of the variable to that method.
相当简单的解决方案,但它确实有效;) 还要注意,cancelDialog 是一个活动擦除变量,如果您不需要从其他地方调用它,那么您应该能够将变量的范围限制为那个方法。
回答by Harry
The hardware key is the answer here. I'd be careful about generalising the DRY principle to UIs. There are plenty of cases where you need to hammer, hammer, hammer the same point to the user repeatedly via headings, body text, colours and images.
硬件密钥是这里的答案。我会小心将 DRY 原则推广到 UI。在很多情况下,您需要通过标题、正文、颜色和图像向用户反复敲打、敲打、敲打同一点。
Users dont "read" UIs the way you read a novel. They scan read.
用户不会像阅读小说那样“阅读”用户界面。他们扫描阅读。
回答by Will
I can't speak for other apps, but in mine anything that might cause the UI thread to wait is executed in a seperate thread. The most I'll do is show a small progress spinner in the titlebar to let the user know something is going on in the background.
我不能说其他应用程序,但在我的应用程序中,任何可能导致 UI 线程等待的东西都是在单独的线程中执行的。我要做的最多的是在标题栏中显示一个小的进度微调器,让用户知道后台正在发生什么。
回答by Hamid
As an Android user, and developer, I can say, in my opinion, and based around my understanding of the platform, that there is a good reason for not having a cancel button by default on the cancel-free progress dialogs.
作为 Android 用户和开发人员,我可以说,在我看来,基于我对平台的理解,有一个很好的理由默认情况下在无取消进度对话框上没有取消按钮。
As a developer, these dialogs can not be cancelled by default, that is, you have to explicitely set them as cancelable.
作为开发者,这些对话框默认是不能取消的,也就是你必须明确设置为可取消。
This makes sense, because their purpose is to alert the user, via the UI thread, that some work is going on elsewhere that is important to the updating of the UI thread, before the user should continue their use of the application.
这是有道理的,因为它们的目的是通过 UI 线程提醒用户,在用户应该继续使用应用程序之前,其他地方正在进行一些对 UI 线程更新很重要的工作。
For example, when fetching a list of data to occupy an empty screen, there is nothing to interact with, and the user needs to be made aware that something is going on, and to expect there to be nothing available to interact with until this process is complete.
例如,当获取一个数据列表以占据一个空屏幕时,没有什么可以交互的,并且用户需要知道正在发生的事情,并期望在这个过程之前没有任何可用的交互做完了。
However, there may be cases, such as data retrieval from the internet, where the process is "sketchy" and due to connectivity issues, you may not be able to complete the request, and get stuck here.
但是,在某些情况下,例如从 Internet 检索数据,该过程是“粗略的”,并且由于连接问题,您可能无法完成请求,并卡在此处。
This as a develop is where you enable the dialog to be cancel-able.
这作为开发是您启用对话框可取消的地方。
Now as a user, one that clearly understands the UI paradigm of Android, I know that if I want to go back to what I was doing before, or "cancel" the current operation, I should hit the back key.
现在作为一个清楚了解Android UI范式的用户,我知道如果我想回到我之前在做的事情,或者“取消”当前的操作,我应该点击返回键。
As a user, it's no different to knowing that in Android, the menu key can often reveal hidden options, some times on a seemingly blank screen with no way to interact.
作为用户,知道在 Android 中,菜单键通常可以显示隐藏的选项,有时在看似空白的屏幕上无法进行交互,这并没有什么不同。
This is the behaviour a user expects, and the way the platform is designed. You could very well add a cancel button, but from a users perspective that understands their phone, this makes no difference, the back key is clearly the intended choice for this purpose.
这是用户期望的行为,也是平台的设计方式。您可以很好地添加取消按钮,但从了解他们手机的用户的角度来看,这没有区别,后退键显然是用于此目的的预期选择。
Activities and UIs have a flow, you flow through activities and UI "screens" which are "stacked" and the back button essentially "pops" the last thing off the stack to return you to where you were previously. If you see the dialog as another of these activities, you want to pop it from the top of the stack to return to what is underneath, or an activity previous to that.
活动和 UI 有一个流程,您流经“堆叠”的活动和 UI“屏幕”,而后退按钮基本上“弹出”堆栈中的最后一个东西,让您返回到之前的位置。如果您将对话框视为这些活动中的另一个,您希望将其从堆栈顶部弹出以返回到下面的内容或之前的活动。
If a developer has a dialog that can not be cancelled by back, it is either, by design, for which there can, in cases, be very good reason for, or, it is poor development and an oversight on the devloper's part.
如果开发人员有一个无法通过返回取消的对话,那么这要么是设计使然,在某些情况下,这可能有很好的理由,要么是开发不良和开发人员的疏忽。

