2013-04-08 69 views
105
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; 

     } 
    } 

我想在改变背景之间的命令之间设置一个延迟。我尝试使用线程计时器并尝试使用run和catch。但它不起作用。我试过这个如何在android中设置延迟?

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

但它只是变成了黑色。

回答

336

尝试此代码:

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); 
+24

远远高于了Thread.sleep更好的()。 – 2013-04-08 08:13:19

+0

它工作。谢谢 – 2013-04-08 08:38:55

+0

该解决方案解释了我在处理某些代码行时遇到的所有问题。 – Sierisimo 2015-05-19 17:55:21

12

使用Thread.sleep(millis)方法。

+16

不要在UI线程上执行此操作 - 其他元素也可能会停止响应,并在以后出现不可预知的行为 – jmaculate 2014-05-19 18:23:10

+0

感谢您的警告。这正是我需要的,以延迟UI线程。满足我需求的完美答案。谢谢。 – hamish 2014-06-06 20:15:21

28

您可以使用CountDownTimer,这比发布的任何其他解决方案效率更高。您也可以产生沿途间隔定期通知使用其onTick(long)方法

看一看这个例子显示如果你想要做在UI上的东西一定的时间间隔非常一个30秒倒计时

new CountDownTimer(30000, 1000) { 
     public void onFinish() { 
      // When timer is finished 
      // Execute your code here 
    } 

    public void onTick(long millisUntilFinished) { 
       // millisUntilFinished The amount of time until finished. 
    } 
    }.start(); 
+0

然后,你将不得不取消定时器 – peter 2016-09-20 07:22:31

1

好的选择是使用CountDownTimer:

new CountDownTimer(30000, 1000) { 

    public void onTick(long millisUntilFinished) { 
     mTextField.setText("seconds remaining: " + millisUntilFinished/1000); 
    } 

    public void onFinish() { 
     mTextField.setText("done!"); 
    } 
    }.start(); 
17

如果您在应用程序中使用延迟频繁,使用这个工具类

import android.os.Handler; 

/** 
* Author : Rajanikant 
* Date : 16 Jan 2016 
* Time : 13:08 
*/ 
public class Utils { 

    // Delay mechanism 

    public interface DelayCallback{ 
     void afterDelay(); 
    } 

    public static void delay(int secs, final DelayCallback delayCallback){ 
     Handler handler = new Handler(); 
     handler.postDelayed(new Runnable() { 
      @Override 
      public void run() { 
       delayCallback.afterDelay(); 
      } 
     }, secs * 1000); // afterDelay will be executed after (secs*1000) milliseconds. 
    } 
} 

用法:

// Call this method directly from java file 

int secs = 2; // Delay in seconds 

Utils.delay(secs, new Utils.DelayCallback() { 
    @Override 
    public void afterDelay() { 
     // Do something after delay 

    } 
}); 
+0

这是我正在寻找.. – Qasim 2016-07-14 08:27:39