繁体   English   中英

Thread.sleep停止整个函数android

[英]Thread.sleep stopping entire function android

我有一个java函数,我想在中间延迟。

frombox.setText(simpchi[rannum] + "\n[" + pinyin[rannum] + "]");
String meaning = meanings[rannum];
try {
        Thread.sleep(500);
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        e.printStackTrace();
    }
tobox.setText(meaning.replace("/", "\n"));

我希望更改收件箱的文本,然后在0.5秒后更改收件箱的文本。

但是,执行此操作时,整个函数会延迟,然后发件箱和收件箱的文本会同时更改。

我究竟做错了什么? 对不起,如果这很简单; 我是java的新手。

您不应该在UI线程中调用Thread.sleep() 决不! 你应该做什么:

Handler handler = new Handler();
handler.postDelayed(new Runnable(){ 
    public void run(){
        tobox.setText(meaning.replace("/", "\n"));
    }
}, 500); // 500 ms

或简单地(致zapl)

tobox.postDelayed(new Runnable(){ 
    public void run(){
        tobox.setText(meaning.replace("/", "\n"));
    }
}, 500); // 500 ms

这样第二个settext将被延迟并在UI线程中运行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM