wait for 3 seconds or user click

后端 未结 2 1677
生来不讨喜
生来不讨喜 2020-12-08 05:16

I am trying to set up a situation where I am waiting for a small period of time say 3 seconds and move on. But if the user clicks my on-screen button then move on as I would

相关标签:
2条回答
  • 2020-12-08 05:59

    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
    }
    
    0 讨论(0)
  • 2020-12-08 06:11

    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.

    0 讨论(0)
提交回复
热议问题