在android中等(Wait in android)

编程入门 行业动态 更新时间:2024-10-07 19:24:43
android中等(Wait in android)

我正在开发一个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);

更多推荐

本文发布于:2023-08-07 18:14:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465269.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:android   Wait

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!