Android:定时后启动监听器

发布于 2024-12-06 21:38:35 字数 171 浏览 7 评论 0原文

我有一个带有 ToggleButton 的活动,必须启动(如果选中)或停止(如果选中)侦听器。 问题是我想在特定时间(例如 10 秒)后启动侦听器,但保持 ToggleButton 处于活动状态,如果用户在计时结束之前单击它,则中止计时和侦听器激活。 我对正确的方法感到困惑......你有什么想法吗?

提前致谢

i've an activity with a ToggleButton that must start (if checked on) or stop (if checked off) a listener.
The problem is that i want to start the listener after a specific time (10 sec for example) but keeping the ToggleButton active and if the user click on it before the timing ending, abort the timing and listener activation.
I'm in confusion with the correct way to do that... do you have any idea?

thanks in advance

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

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

发布评论

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

评论(1

甜是你 2024-12-13 21:38:35

使用计时器和计时器任务。
将计时器设置为 10 秒延迟。

如果用户在这 10 秒内再次单击按钮,则取消计时器。
如果没有,启动监听器。

请注意,为了通过 TimerTask 线程更改 UI 元素,您必须使用处理程序。

Use a Timer and TimerTask.
Put the timer on 10 seconds delay.

If the user clicks the button again on these 10 seconds, cancel the Timer.
If not, start the listener.

Note that in order to change UI elements through the TimerTask thread, you will have to use an handler.

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