如何按顺序延迟更改按钮颜色

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

标签: java android

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_game);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    take = getIntent();
    levelone = take.getIntArrayExtra("level1");
    take = getIntent();
    leveltwo = take.getIntArrayExtra("level2");
    take = getIntent();
    levelthree = take.getIntArrayExtra("level3");
    colors[0] = (R.drawable.blue1);//Suppose to give integer value for the colors
    colors[1] = (R.drawable.purple1);
    colors[2] = (R.drawable.yellow1);
    colors[3] = (R.drawable.green1);
    colors1[0] = (R.drawable.blue);//Suppose to set the colors back to origin
    colors1[1] = (R.drawable.purple);
    colors1[2] = (R.drawable.yellow);
    colors1[3] = (R.drawable.green);


    purple = findViewById(R.id.purplee);   //1
    green = findViewById(R.id.greenn);//2
    yellow = findViewById(R.id.yelloww);//3
    blue = findViewById(R.id.bluee);//4
  for (int i = 0; i < btn.length; i++) {
        buttons[i] = findViewById(btn[i]);
        buttons[i].setOnClickListener(this);



    }


    /*new CountDownTimer(5000,1000)//5000=5sec to wait and 1000=1sec for interval
    {
        // loop for timer
        @Override
        public void onTick(long l) {
            Toast.makeText(Main.this, ""+l/1000, Toast.LENGTH_SHORT).show();
        }

        //what happend after finish 5 sec
        @Override
        public void onFinish() {
            Intent go=new Intent(Main.this,Start.class);
            startActivity(go);

        }
    }.start();*/


    new CountDownTimer(2000,500)//5000=5sec to wait and 1000=1sec for interval
    {
        // loop for timer
        @Override
        public void onTick(long l) {
            buttons[1].setBackgroundResource(colors1[1]);//purple Butttons[1]
            buttons[2].setBackgroundResource(colors1[2]);//Yellow Buttons[2]
            buttons[3].setBackgroundResource(colors1[3]);//Green Buttons[3]
            buttons[0].setBackgroundResource(colors1[0]);//Blue Buttons[0]
        }

        //what happend after finish 5 sec
        @Override
        public void onFinish() {



        }
    }.start();

我想按延迟更改按钮的颜色,所以我运行了几个选项,但没有一个起作用,我的意思是我没有看到延迟,按钮的颜色也没有像预期的那样变化所以我应该怎么做?在代码中,我尝试了一些操作,但是没有用,因此,如果您有任何想法,我将很高兴听到。

2 个答案:

答案 0 :(得分:2)

您可以使用处理程序

Handler handler = new Handler();
    for(int i=0; i>btn.size; i++){
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                buttons[i].setBackgroundResource(colors1[i]);
            }
        },1000); // Delay every "1" second
    }

答案 1 :(得分:0)

//Write This Code Inside onCreate Methode
Timer timer = new Timer();
MyTimer myTimer = new MyTimer();
timer.schedule(myTimer, 1000, 1000);

//Make this Class Outside onCreate Methode
 class MyTimer extends TimerTask {
    @Override
    public void run() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Random random = new Random();
                for (int i = 0; i < btn.size; i++) {
                    buttons[i].setBackgroundColor(Color.argb(255, random.nextInt(256), random.nextInt(256), random.nextInt(256)));
                }
            }
        });
    }
}