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) 中。但是在用户按下正确的颜色后,颜色不会变回正常。
最佳答案
你的做法正好相反。你必须在按下 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);
}
关于java - 西蒙说按下按钮后按钮颜色不会恢复正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50160568/