通过足够快地单击按钮两次来绕过模态 Android 对话框?
假设我们有两个按钮,每个按钮都有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
单击后禁用该按钮,直到可以安全地再次单击为止。
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 toshow()
. It will occur moments later.