【问题标题】:wait for 3 seconds or user click等待 3 秒或用户点击
【发布时间】:2011-04-26 13:46:55
【问题描述】:

我正在尝试设置一种情况,让我等待一小段时间,比如 3 秒,然后继续前进。但是,如果用户点击我的屏幕按钮,那么我会继续前进。这两个事件将触发相同的行为,即更新屏幕上的文本。有什么想法吗??

Android 新手,但很熟悉它

提前致谢

【问题讨论】:

    标签: android


    【解决方案1】:

    试试这样的:

    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 秒以继续,但如果用户触摸屏幕,则会通知线程并停止等待。

    【讨论】:

    • 抱歉,mSpashThread 实际上是线程,忘记更新该引用以使代码尽可能通用
    【解决方案2】:

    试试下面的,

    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
    }
    

    【讨论】:

    • 请不要对多个问题发布相同的答案 - 它们要么不完全匹配,要么问题是重复的,应该这样标记
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 2010-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-09
    • 2012-11-25
    相关资源
    最近更新 更多