通过足够快地单击按钮两次来绕过模态 Android 对话框?

发布于 2024-11-01 00:28:19 字数 878 浏览 7 评论 0原文

假设我们有两个按钮,每个按钮都有一个 OnClickListener。每个侦听器都会显示一个 ProgressDialog 并执行一些后台工作。 (幕后是一个AsyncTask,对话框是在onPreExecute中打开的。我认为这并不重要,只是为了记录......)。假设有一条规则规定在任何给定时间最多只能有一个后台工作人员处于活动状态。

我的假设是对话框会阻止两个后台工作人员同时运行。我认为模式对话框会阻塞 UI,并且在调用对话框的 show() 方法后无法单击另一个按钮。我错了。

如果您单击按钮的速度足够快,则可以(几乎)同时触发两个后台工作人员。日志显示,尽管存在对话框,但仍可以在 150 毫秒的时间跨度内单击两个按钮:

04-14 18:34:04.390: DEBUG/greenrobot(1860): Clicked: 2131034112
04-14 18:34:04.470: DEBUG/greenrobot(1860): doInBackground2: 2131034112
04-14 18:34:04.540: DEBUG/greenrobot(1860): Clicked: 2131034113
04-14 18:34:04.570: DEBUG/greenrobot(1860): doInBackground2: 2131034113

对话框代码如下所示:

progressDialog = new ProgressDialog(currentActivity);
progressDialog.setMessage(msg);
progressDialog.show(); 

我错过了什么?我希望我错过了一些非常愚蠢的事情,因为如果没有,我想不出一个好的解决方案来阻止点击后的 UI 交互。同步后台工作人员并不是一个解决方案,因为 UI 和场景更加复杂。

Let's say we have two buttons, each with a OnClickListener. Each of listeners show a ProgressDialog and do some background work. (Behind the scene is an AsyncTask, the dialog is opened in onPreExecute. I don't think it matters, just for the record...). Let's say there is some rule saying no more than one background worker may be active at any given time.

My assumption was that the Dialog prevents two background workers running at the same time. I thought the modal dialog blocks the UI and it's not possible to click another button after the show() method of the dialog is called. I was wrong.

If you click the buttons fast enough, it's possible to trigger both background workers (almost) at the same time. The log shows that it's possible to click two Buttons within a 150 ms time span despite the Dialog:

04-14 18:34:04.390: DEBUG/greenrobot(1860): Clicked: 2131034112
04-14 18:34:04.470: DEBUG/greenrobot(1860): doInBackground2: 2131034112
04-14 18:34:04.540: DEBUG/greenrobot(1860): Clicked: 2131034113
04-14 18:34:04.570: DEBUG/greenrobot(1860): doInBackground2: 2131034113

The dialog code looks like this:

progressDialog = new ProgressDialog(currentActivity);
progressDialog.setMessage(msg);
progressDialog.show(); 

What did I miss? I hope I missed something really stupid, because if not, I cannot think of a nice and solution preventing UI interaction after the click. Synchronizing the background workers is not a solution because the UI and scenario is more complex.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝天白云 2024-11-08 00:28:19

单击后禁用该按钮,直到可以安全地再次单击为止。

show() 是异步的。调用 show() 后,对话框不会立即出现。它会在稍后发生。

Disable the button after it is clicked, until it is safe to be clicked again.

show() is asynchronous. The dialog will not appear immediately upon the call to show(). It will occur moments later.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文