我正在开发一个Android项目,我需要让程序等待5秒才能显示一条消息,然后再继续。 我环顾四周,试过线程睡眠(mili); 但它没有用,所以...我包含了我的代码,我只是做错了什么或我还能做什么?
... text.setText("You picked a goat"); lose++; loser.setText("You have picked a goat " + lose + " time(s)"); Thread time = new Thread() { @Override public void run(){ try { synchronized(this){ wait(5000); } } catch(InterruptedException ex){} } }; time.start();...
我也试过把它放在自己的方法中并让它被调用。 失败只是一个反击,“文本”和“失败者”只是文本视图
I am working on an Android project and I need to have the program wait for 5 seconds to display a message and then move on. I have looked around and I tried the Thread sleep(mili); but it didn't work so... I included my code and am I just doing something wrong or what else can I do?.
... text.setText("You picked a goat"); lose++; loser.setText("You have picked a goat " + lose + " time(s)"); Thread time = new Thread() { @Override public void run(){ try { synchronized(this){ wait(5000); } } catch(InterruptedException ex){} } }; time.start();...
I also tried putting this in its own method and having it be called. The lose is just a counter and the "text" and "loser" are just textviews
最满意答案
一种方法是使用Handler类,它允许您在给定的毫秒数后调用Runnable 。 这对于短时间跨距非常有用,就像你的情况一样。
例如,使用postDelayed()方法:
new Handler().postDelayed(new Runnable() { @Override public void run() { //do your stuff here. } }, 5000);One way to do this is to use the Handler class, which will allow you to call a Runnable after a given number of milliseconds. This is useful for short timespans, like in your case.
For example, using the postDelayed() method:
new Handler().postDelayed(new Runnable() { @Override public void run() { //do your stuff here. } }, 5000);更多推荐
发布评论