How to pause / sleep thread or process in Android?

后端 未结 12 873
故里飘歌
故里飘歌 2020-11-22 05:49

I want to make a pause between two lines of code, Let me explain a bit:

-> the user clicks a button (a card in fact) and I show it by changing the background of thi

12条回答
  •  旧巷少年郎
    2020-11-22 06:40

    This is what I did at the end of the day - works fine now :

    @Override
        public void onClick(View v) {
            my_button.setBackgroundResource(R.drawable.icon);
            // SLEEP 2 SECONDS HERE ...
            final Handler handler = new Handler(); 
            Timer t = new Timer(); 
            t.schedule(new TimerTask() { 
                    public void run() { 
                            handler.post(new Runnable() { 
                                    public void run() { 
                                     my_button.setBackgroundResource(R.drawable.defaultcard); 
                                    } 
                            }); 
                    } 
            }, 2000); 
        }
    

提交回复
热议问题