gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 01:37:28 27 4
gpt4 key购买 nike

 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();

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

最佳答案

您可以使用处理程序

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
}

关于java - 如何按顺序延迟更改按钮颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55017160/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com