2011-04-26 101 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