【问题标题】:Simon Says button color won't return to normal after button press西蒙说按下按钮后按钮颜色不会恢复正常
【发布时间】: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 在一秒钟后运行。
  • 另外,而不是四个如果你可以例如创建一个资源 ID 数组,然后根据 newColour 从该数组中查找资源 ID,例如将 normal_buttons 值设置为 0。您应该使用 Log.d() 或类似名称,而不是 println。我还以为游戏只是 "Simon" - “Simon Says”是另外一回事?
  • 哦,你可以声明你的参数 'final int color' 来为闭包提供一个 final 变量,而不必将它复制到 newColor 中。

标签: java android android-drawable android-handler postdelayed


【解决方案1】:

你是在做相反的事情。你必须在按下它后立即改变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);
}

【讨论】:

  • 解决了!感谢您提供详细而有帮助的回复。
【解决方案2】:

我认为函数的流程不正确。因为后延迟方法在 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);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-27
    • 2020-04-20
    • 1970-01-01
    • 1970-01-01
    • 2015-10-01
    • 1970-01-01
    相关资源
    最近更新 更多