每个视图都有post和postDelayed方法分别将一个runnable发布到UI线程或发布延迟它.
button.postDelayed(new Runnable() {
@Override
public void run() {
// change color in here
}
}, 10);
Run Code Online (Sandbox Code Playgroud)
编辑: 如果你经常打电话给这个,你可以用这样的东西做得更好:
int currentColor;
private Runnable changeColorRunnable = new Runnable() {
@Override
public void run() {
switch(currentColor){
case Color.RED: currentColor = Color.BLACK; break;
case Color.BLACK: currentColor = Color.RED; break;
}
button.setBackgroundColor(currentColor);
}
};
Run Code Online (Sandbox Code Playgroud)
然后:
button.postDelayed(changeColorRunnable, 10);
Run Code Online (Sandbox Code Playgroud)
这样可以避免不必要的对象创建和垃圾回收