等待3秒或用户点击

时间:2011-04-26 13:46:56

标签: android

我正试图建立一种情况,我等待一小段时间说3秒然后继续前进。但是,如果用户点击我的屏幕按钮,那么继续前进,无论如何。这两个事件都会触发相同的行为,即更新屏幕上的文本。有什么想法??

Android新手但mclovin'它

提前致谢

2 个答案:

答案 0 :(得分: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秒钟继续,但如果用户触摸屏幕,则会通知线程并停止等待。

答案 1 :(得分:-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
}