2013-04-08 99 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