简体   繁体   English

等待3秒钟或用户单击

[英]wait for 3 seconds or user click

I am trying to set up a situation where I am waiting for a small period of time say 3 seconds and move on. 我正在尝试设置一种情况,我在等待一小段时间(例如3秒钟)然后继续前进。 But if the user clicks my on-screen button then move on as I would have anyway. 但是,如果用户单击我的屏幕上的按钮,则可以继续进行操作。 Both events will trigger the same behaviour namely to update text on the screen. 这两个事件将触发相同的行为,即更新屏幕上的文本。 Any thoughts?? 有什么想法吗??

New to Android but mclovin' it Android的新手,但mclovin'

Thanks in advance 提前致谢

Try something like this: 尝试这样的事情:

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

It waits 3 seconds to continue but if the user touches the screen the thread is notified and it stops waiting. 它等待3秒钟才能继续,但是如果用户触摸屏幕,则该线程将收到通知,并且它将停止等待。

Try the following, 尝试以下方法

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM