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)中。但是,在用户按下正确的颜色后,颜色不会恢复正常。
答案 0 :(得分:2)
你是在对面做的。你必须在按下之后立即更改Button
的颜色,你必须将你的返回颜色保持在postDelayed
以便在延迟之后给定时间它变成正常颜色。
public void flashButton(int color) {
final ImageView colors = findViewById(R.id.buttonsImage);
final int newColor = color;
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 handler = new Handler();
final Runnable r = new Runnable() {
public void run() {
colors.setImageResource(R.drawable.normal_buttons);
System.out.println("Returned Color.");
}
};
handler.postDelayed(r, 1000);
}
答案 1 :(得分:0)
我认为该功能的流程不正确。因为后延迟方法在1秒后执行。在此之前,将执行colors.setImageResource(R.drawable.normal_buttons)方法。改变流程如下
public void flashButton(int color) {
final ImageView colors = findViewById(R.id.buttonsImage);
final int newColor = color;
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);
Handler handler = new Handler();
final Runnable r = new Runnable() {
public void run() {
colors.setImageResource(R.drawable.normal_buttons);
}
};
handler.postDelayed(r, 1000);
}