Android:定时后启动监听器
我有一个带有 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用计时器和计时器任务。
将计时器设置为 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.