public void onClick(View v) { // TODO Auto-generated method stub switch(v.getId()){ case R.id.rollDice: Random ranNum = new Random(); int number = ranNum.nextInt(6) + 1; diceNum.setText(""+number); sum = sum + number; for(i=0;i<8;i++){ for(j=0;j<8;j++){ int value =(Integer)buttons[i][j].getTag(); if(value==sum){ inew=i; jnew=j; buttons[inew][jnew].setBackgroundColor(Color.BLACK); //I want to insert a delay here buttons[inew][jnew].setBackgroundColor(Color.WHITE); break; } } } break; } }
我想在更改背景之间的命令之间设置延迟。我尝试使用线程计时器,并尝试使用运行和捕获。但这不起作用。我试过了
Thread timer = new Thread() { public void run(){ try { buttons[inew][jnew].setBackgroundColor(Color.BLACK); sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } } }; timer.start(); buttons[inew][jnew].setBackgroundColor(Color.WHITE);
但这只是变成黑色。
试试这个代码:
import android.os.Handler; ... final Handler handler = new Handler(); handler.postDelayed(new Runnable() { @Override public void run() { // Do something after 5s = 5000ms buttons[inew][jnew].setBackgroundColor(Color.BLACK); } }, 5000);