Android处理程序定期 [英] Android Handler Periodically
问题描述
我想实现的目标:
-
活动从没有ClickListener开始,并且具有四个带有白色背景的文本视图
An activity starts with no ClickListener and has four textviews all with white background
我想将textview 1的颜色更改为蓝色.等待2秒钟,然后将其更改为白色,然后将textview 2更改为蓝色.等待2秒钟,然后将其更改为白色...等等,直到我将textview 4更改为蓝色,然后又更改为白色.
I want to change the color of textview 1 to blue. Wait for 2 seconds and then change it back to white and change the textview 2 to blue. wait for 2 seconds and then change it back to white... so on till i have turned textview 4 to blue and back to white.
完成后,我想添加ClickListener并等待用户输入.
Once that is complete, I want to add the ClickListener and wait for user input.
我该如何实现?我是Android的新手,但了解点点滴滴.
How can I achieve this? I am new to Android but understands bits and pieces.
推荐答案
无需为此创建线程或动画.
There is no need to create a thread for this, or animations.
解决方案非常简单:使用Handler.postDelayed()或Handler.sendMessageDelayed():
The solution is really simple: use Handler.postDelayed() or Handler.sendMessageDelayed():
http://developer.android. com/reference/android/os/Handler.html#postDelayed(java.lang.Runnable ,长) http://developer.android.com/reference/android/os/Handler.html#sendMessageDelayed(android.os.Message ,长)
为实现可靠的实现,请确保至少通过Activity.onDestroy()删除所有待处理的消息. (或者,如果要在Activity.onStart()中发布它们,请在Activity.onStop()中将其删除;如果要在Activity.onResume()中发布,则将其在Activity.onPause()中删除.)
For a robust implementation, be sure to remove any pending messages at least by Activity.onDestroy(). (Or if you are posting them in Activity.onStart(), remove them in Activity.onStop(); if posting in Activity.onResume(), remove in Activity.onPause().)
这篇关于Android处理程序定期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!