我想制作一个没有ontouch方法的布局背景,每1秒更换一次颜色,而不会停止,直到按下后退按钮,如红白绿,红绿白等。
答案 0 :(得分:1)
你考虑使用PostDelayed()吗?
public class MyView implements Runnable
{
public void MyInit ()
{
... set initial background ...
postDelayed (this, 1000); // delay a second - cause "run" to execute in 1 sec.
}
public void run()
{
... toggle background ...
postDelayed (this, 1000); // delay another second
}
}
答案 1 :(得分:1)
您可以使用Java代码View.setBackground(int color)更改背景,然后根据new Date().getTime()%3
的结果使用Handler
进行changeBackground。
例如,类似的东西会起作用:
final Handler handler = new Handler();
Runnable runnable = new Runnable() {
@Override
public void run() {
int color = Color.RED;
switch(new Date().getTime()%3){
case 0:
color = Color.RED;
break;
case 1:
color = Color.WHITE;
break;
case 2:
color = Color.GREEN;
break;
}
yourView.setBackground(color);
handler.postDelayed(this, 1000);
}
};
handler.postDelayed(runnable, 1000);