【发布时间】:2018-10-14 02:15:05
【问题描述】:
public void flashButton(int color) {
final ImageView colors = findViewById(R.id.buttonsImage);
final int newColor = color;
Handler handler = new Handler();
final Runnable r = new Runnable() {
public void run() {
if(newColor == 1)
colors.setImageResource(R.drawable.green_activated_png);
if(newColor == 2)
colors.setImageResource(R.drawable.yellow_activated_png);
if(newColor == 3)
colors.setImageResource(R.drawable.red_activated_png);
if(newColor == 4)
colors.setImageResource(R.drawable.blue_activated_png);
System.out.println("Flashed color: " + newColor);
}
};
handler.postDelayed(r, 1000);
colors.setImageResource(R.drawable.normal_buttons);
System.out.println("Returned Color.");
}
使用 R.drawable.green_activated_png 为每个按钮更改按钮颜色)。然后,我用 (R.drawable.normal_buttons) 把它改回来。我认为我的问题出在 handler.postDelayed(r, 1000) 中。但是在用户按下正确的颜色后,颜色并没有恢复正常。
【问题讨论】:
-
不,你把它弄反了:你正在执行最后两行,即正常,立即,然后排队 r 在一秒钟后运行。
-
哦,你可以声明你的参数 'final int color' 来为闭包提供一个 final 变量,而不必将它复制到 newColor 中。
标签: java android android-drawable android-handler postdelayed