这里是代码
Button change = (Button) findViewById(R.id.btn_change);
change.setBackgroundResource(R.drawable.button_red);
...
change.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
v.setBackgroundResource(R.drawable.button_orange);
try {
Thread.sleep(5000, 0);
} catch (InterruptedException e) {
e.printStackTrace();
}
v.setBackgroundResource(R.drawable.button_green);
}
});
答案 0 :(得分:3)
在Android中使用case = [case] if not isinstance(case, list) else case
是非常糟糕的做法,尤其是在主UI线程上。改为使用计时器或后期运行(见下文)。
Thread.sleep()
答案 1 :(得分:0)
考虑使用Animation Drawable。
在你的情况下,你要定义你的drawable:
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="true">
<item android:drawable="@color/red" android:duration="0" />
<item android:drawable="@color/orange" android:duration="5000" />
<item android:drawable="@color/green" android:duration="0" />
</animation-list>
这样说,“当它开始播放时开始变红,立即切换到橙色5秒,然后切换到绿色。”
所以在你的代码中:
Button change = (Button) findViewById(R.id.btn_change);
// This starts you off red
change.setBackgroundResource(R.drawable.color_animation);
...
change.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
AnimationDrawable ad = (AnimationDrawable) v.getBackground();
// Start the animation, which will switch to orange immediately for 5 seconds, then turn green
ad.start();
}
});