2011-04-26 104 views
17

我正在嘗試設置一個等待一小段時間的情況,比如說3秒鐘,然後繼續前進。但是,如果用戶點擊我的屏幕上的按鈕,然後像我反正一樣繼續前進。這兩個事件都會觸發相同的行爲,即更新屏幕上的文本。有什麼想法嗎??等待3秒鐘或用戶點擊

新到Android,但mclovin'它

在此先感謝

回答

26

嘗試是這樣的:

private Thread thread;  

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.layoutxml); 

    final MyActivity myActivity = this; 

    thread= new Thread(){ 
     @Override 
     public void run(){ 
      try { 
       synchronized(this){ 
        wait(3000); 
       } 
      } 
      catch(InterruptedException ex){      
      } 

      // TODO    
     } 
    }; 

    thread.start();   
} 

@Override 
public boolean onTouchEvent(MotionEvent evt) 
{ 
    if(evt.getAction() == MotionEvent.ACTION_DOWN) 
    { 
     synchronized(thread){ 
      thread.notifyAll(); 
     } 
    } 
    return true; 
}  

它等待3秒繼續,但如果用戶觸摸屏幕的線程通知並停止等待。

+0

請您提供mSplashThread' – WarrenFaith 2011-04-26 14:13:03

+0

對不起的'定義,mSpashThread竟是線程,忘了更新參考使得代碼一般爲更多鈔票 – ferostar 2011-04-26 14:18:51

+1

這就是我想要的,謝謝ferostar! – 2013-01-01 20:52:45

-3

嘗試以下,

button = (Button) findViewById(R.id.buttonView); 
button.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     Runnable clickButton = new Runnable() { 
      @Override 
      public void run() { 
       // whatever you would like to implement when or after clicking button 
      } 
     }; 
     button.postDelayed(clickButton, 3000); //Delay for 3 seconds to show the result 
} 
+0

請不要發佈相同的答案几個問題 - 他們要麼不完全適合或問題是重複的,應該被標記爲這樣 – kleopatra 2014-02-08 08:31:45