每秒更改按钮的颜色

时间:2019-06-06 23:03:36

标签: java android

我想建立一个反应游戏,当它是正确的颜色(红色)时,您必须单击一个变色按钮。我该如何显示颜色,然后再将其更改为另一种随机颜色,直到出现正确的颜色。

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_farberkennung);

        b_zurück = (Button) findViewById(R.id.b_zurück);
        b_start3 = (Button) findViewById(R.id.b_start3);
        b_randomColor = (Button) findViewById(R.id.b_randomcolor);

        final int background;
        final Random rand = new Random();
        final List<Integer> list = new ArrayList<>();
        list.add(getResources().getColor(R.color.red));
        list.add(getResources().getColor(R.color.green));
        list.add(getResources().getColor(R.color.yellow));
        //list.add(getResources().getColor(R.color.blue));
        //list.add(getResources().getColor(R.color.orange));
        //list.add(getResources().getColor(R.color.olive));
        //list.add(getResources().getColor(R.color.purple));
        background = list.get(rand.nextInt(list.size()));

        b_start3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                b_start3.setEnabled(false);
                b_randomColor.setBackgroundColor(background);

//loop random colours till red appears

                while (background != getResources().getColor(R.color.red)) {
                    Handler handler = new Handler();
                    handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            b_randomColor.setBackgroundColor(background);
                        }

                    }, 1000);
                }

                startTime = System.currentTimeMillis();
            }
        });

        b_randomColor.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (background == (getResources().getColor(R.color.red))) {
                    endTime = System.currentTimeMillis();
                    currentTime = endTime - startTime;
                    b_randomColor.setText(currentTime + " MS");
                    b_randomColor.setEnabled(false);
                    b_start3.setEnabled(true);
                }
            }
        });```

1 个答案:

答案 0 :(得分:0)

您可以通过以下方式进行操作: 创建一个名为 Timer

的新Java类文件
public class Timer {

  private thread t ;
  public boolean runTimer ;
  public long delay ;
  public int msg = 0;
  private Handler handler ;

  public Timer(Handler handler , long delay , int msg){
      this.handler = handler ;
      runTimer = true ;
      this.delay = delay ;
      this.msg = msg ;
      t = new thread();
      t.start();
  }

  public class thread extends Thread {

      @Override
      public void run(){
          while (runTimer){
              try{
                  thread.sleep(delay);
              }catch (Exception e){}

              Message message = handler.obtainMessage(msg);
              handler.sendMessage(message);
          }
      }
  }

  public void playTimer (boolean play){
      runTimer = play ;
  }

}

然后将此消息处理程序添加到您的活动中,并在该处理程序中,您可以测试背景颜色是否为红色

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    .........
}
// here the code you add to your activity
private final Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        if (background != getResources().getColor(R.color.red)){
           b_randomColor.setBackgroundColor(background);
        }else {
           // you could stop your timer thread like that ...
           // timer.playTimer(false);
        }
    }
};

最后像这样启动计时器:

 final Timer timer;
 timer = new Timer(handler , 1000 , "time");