简体   繁体   English

Dialog.show()是一种非阻塞方法吗?

[英]Is Dialog.show() a non-blocking method?

I've got a button that kicks off a background thread to do some work and I am trying to use a ProgressDialog to prevent the user from double clicking that button (or any other ui elements) while that work is being done. 我有一个按钮,开始后台线程做一些工作,我正在尝试使用ProgressDialog来防止用户在完成该工作时双击该按钮(或任何其他ui元素)。 The first thing I do in my buttons onClick code is to display the progress dialog, which takes over the screen. 我在按钮onClick代码中做的第一件事是显示进度对话框,它接管屏幕。 The problem I am seeing is that if I rapidly tap this button, sometimes two or more presses will register before the ProgressDialog is shown. 我看到的问题是,如果我快速点击此按钮,有时两次或更多次按下将在显示ProgressDialog之前注册。 This leads me to assume that ProgressDialog.show() is returning before the ProgressDialog is actually visible. 这使我假设ProgressDialog.show()在ProgressDialog实际可见之前返回。

Can anybody confirm this? 任何人都可以证实吗? Also, is there a way to change this behavior, or at least get a notification of when the dialog is actually visible? 此外,有没有办法改变这种行为,或至少得到对话框实际可见的通知? I saw Dialog.onStart() but given the javadoc, this appears to be called before the Dialog is actually visible... 我看到Dialog.onStart()但是给了javadoc,这似乎是在Dialog实际可见之前被调用的......

UPDATE: While it appears that there is no good way of solving this problem in general, the following works for my situation where my work is done by an external thread and the amount of work to do takes longer than the time it takes for all the button clicks to be processed: 更新:虽然看起来通常没有好办法解决这个问题,但以下情况适用于我的工作由外部线程完成的情况,并且要完成的工作量需要的时间比所有工作所需的时间长按钮点击进行处理:

void myOnClickHandler() {
  if(myButton.isEnabled()) {
    myButton.setEnabled(False);
    // do work here
    // setEnabled(true) is invoked at the end of my spawned thread's run().
  }
}

No. 没有。

The problem is you clicked many times before the click event is delivered. 问题是您在单击事件发送之前多次单击。 (ie it is queued before you run ProgressDialog.show() .) (即在运行ProgressDialog.show()之前它已排队。)

From what I've noticed in Android you can double click on a button rapidly and have the onClick listener fire twice (or even more) regardless of the code in the listener (even if you disable the button immediately). 根据我在Android中注意到的,您可以快速双击按钮并使onClick侦听器触发两次(甚至更多),无论侦听器中的代码如何(即使您立即禁用该按钮)。

I reported a bug a while ago here: http://code.google.com/p/android/issues/detail?id=20073 but of course these things tend to go "unnoticed" by Google. 我不久前在这里报告了一个错误: http//code.google.com/p/android/issues/detail?id = 2003,但当然这些事情往往会被谷歌“忽视”。 Feel free to star it in hopes of getting Google's attention 随意加入星球,希望得到谷歌的关注

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM