如何在 ProgressDialog 之后显示 AlertDialog
我在网上搜索但没有找到满意的答案。
我有一项需要一些时间才能完成的任务,因此我使用 ProgressDialog 来通知用户应用程序正在执行某些操作。
如果在长时间任务期间遇到错误,我想显示一个 AlertDialog 让用户知道该错误。
所以我编写了代码,但 ProgressDialog 关闭并且 AlertDialog 不显示。
我使用 onCreateDialog() 方法来管理我的对话框。
有人知道为什么吗?
谢谢。
I searched across the web but I didn't find any satisfying answer.
I have a task that can make some time to complete so I use a ProgressDialog to inform the user that the application is doing something.
If an error is encountred during the long task, I want to display an AlertDialog to let the user know about the error.
So I wrote the code but the ProgressDialog dismisses and the AlertDialog doesn't show.
I use the onCreateDialog() method to manage my dialogs.
Someone knows why?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不确定为什么你的代码不起作用,但如果这是一个计时问题,你可以尝试使用 处理程序 在短暂延迟后调用显示第二个对话框:
这可能会给 UI 线程一些时间来清理第一个对话框,该对话框的存在可能会影响第二个对话框。您也可以尝试使用
postDelayed
来代替较小的时间延迟,例如 100 毫秒。I'm not sure why your code isn't working, but if it's a timing issue you might try using a Handler to call show your second dialog after a short delay:
This may give the UI thread some time to clean up the first dialog, whose presence may be affecting the second. You might also try
postDelayed
instead with a small time delay such as 100 milliseconds.您可以使用以下功能来显示警报对话框。
You can use below function to show alert Dialog .
在打开警报对话框之前,您应该确保进度对话框已完全关闭。我为此使用取消方法。
ProgressDialog.setOnCancelListener(this);
...
public void onCancel(final DialogInterface 对话框) {
显示警报对话框();
}
...
ProgressDialog.cancel()
任务完成后,您将调用 ProgressDialog.cancel();
这将触发 onCancel 方法并打开警报对话框,很好地显示警报输出和动画。
注意:您也可以使用miss()来实现此目的,但它更容易出错。
You should make sure that the progress dialog is fully dismissed before opening the alert dialog. I use the cancel method for that.
progressDialog.setOnCancelListener(this);
...
public void onCancel(final DialogInterface dialog) {
showAlertDialog();
}
...
progressDialog.cancel()
Once you task is complete you will call progressDialog.cancel();
This will trigger the onCancel method and open the alert dialog showing nicely both alert out and in animations.
Note: you can also use dismiss() for this, but it's a bit more error prone.