如何从 postDelayed 添加的处理程序对象中删除可运行对象?
我有一个 “打开” 动画,并使用 Handler.postDelayed(Runnable, delay)
在短暂延迟后触发 “关闭” 动画。然而,在打开和关闭之间的时间里,可能有另一个由点击触发的动画。
我的问题是,如何取消处理程序中的“关闭”动画?
I have an "open" animation and am using Handler.postDelayed(Runnable, delay)
to trigger a "close" animation after a short delay. However, during the time between open and close, there is possibly another animation triggered by a click.
My question is, how would I cancel the "close" animation in the handler?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是一个迟来的答案,但是当您只想从处理程序中删除特定类别的可运行对象时(即在OP的情况下,只需删除关闭动画,将其他可运行对象留在队列中),这里有一种不同的方法:
上面的代码将仅执行“r3”,然后执行“r4”。这使您可以删除由令牌定义的特定类别的可运行对象,而无需保留对可运行对象本身的任何引用。
注意:源代码仅使用“==”操作数来比较标记(它不调用 .equals()),因此最好使用整数/整数而不是字符串作为标记。
This is a late answer, but here's a different method for when you only want to remove a specific category of runnables from the handler (i.e. in OP's case, just remove the close animation, leaving other runnables in the queue):
The above code will execute "r3" and then "r4" only. This lets you remove a specific category of runnables defined by your token, without needing to hold any references to the runnables themselves.
Note: the source code compares tokens using the "==" operand only (it does not call .equals()), so best to use ints/Integers instead of strings for the token.
如果您使用递归,您可以通过传递“this”来实现这一点。请参阅下面的代码。
此示例将每秒设置 TextView(计时器)的文本,倒计时。一旦达到 0,它将从 UI 中删除 TextView 并禁用倒计时。这仅对使用递归的人有用,但我来到这里寻找递归,所以我发布了我的结果。
If your using recursion, you can acheive this by passing "this". See code below.
This example will set the text of a TextView (timer) every second, counting down. Once it gets to 0, it will remove the the TextView from the UI and disable the countdown. This is only useful for someone who is using recursion, but I arrived here searching for that, so I'm posting my results.
如果您使用匿名可运行对象,您可以将其与令牌对象配对,以便稍后将其删除。
要删除它,您需要相同的令牌,只需调用
警告:将使用
==
而不是.equals
方法来比较令牌。最好不要使用原始数据类型,请参阅:未删除令牌类型 Int 或 Long (*Kotlin) 的 Android 处理程序回调If you are using an anonymous runnable you could pair it with a token object to be able to remove it later.
and to remove it you need the same token and just call
WARNING: the token will be compared using
==
and not the.equals
method. Better not use primitive data types, see: Android Handler callback not removed for token type Int or Long (*Kotlin)只需使用
removeCallbacks(Runnable r)
方法。Just use the
removeCallbacks(Runnable r)
method.Cristian 的答案是正确的,但与答案评论中所述相反,您实际上可以通过调用
removeCallbacksAndMessages(null);
删除匿名Runnables
的回调,如所述此处:
Cristian's answer is correct, but as opposed to what is stated in the answer's comments, you actually can remove callbacks for anonymous
Runnables
by callingremoveCallbacksAndMessages(null);
As stated here: