这里是我的代码:在Android中,如何更改按钮的颜色,暂停几秒钟,然后再次更改它?
public class TestActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
start();
}
private void start() {
setContentView(R.layout.main);
LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout);
Button button = new Button(this);
layout.addView(button);
button.getBackground().setColorFilter(new LightingColorFilter(0x00000000, 0x0000FF00)); // green
button.invalidate();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
button.getBackground().setColorFilter(new LightingColorFilter(0x00000000, 0x000000FF)); // blue
button.invalidate();
}
}
这短短3秒后显示一个蓝色按钮;它从不显示为绿色。
我想,如果我的大脑正常工作,我可以从这些职位之一找出答案:
Why isn't view.invalidate immediately redrawing the screen in my android game
How to force a view to redraw immediately before the next line of code is executed
How to force an entire layout View refresh?