使用 setText 更改文本后如何更新屏幕?

编程入门 行业动态 更新时间:2024-10-11 19:15:10
本文介绍了使用 setText 更改文本后如何更新屏幕?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

现在我正在使用以下代码来更新 TextView

Right now I am using the following code to update a TextView

txtMain.setText("new text");

执行该代码后,屏幕不会更新为新文本.有没有办法可以强制文本立即更新?

After that code executes, the screen does not update with the new text. Is there a way I can force the text to update right then and there?

推荐答案

执行该代码后,屏幕不会更新为新文本

After that code executes, the screen does not update with the new text

一旦您将控制权交还给 Android,它就应该如此.

It should, once you return control to Android.

例如,假设您尝试在 setText() 调用后立即执行 Thread.sleep(5000);.文本不会出现在屏幕上,因为 Android 的主应用线程被困在睡眠中,无法重绘屏幕.

Suppose, for example, you tried doing a Thread.sleep(5000); immediately after the setText() call. The text will not appear on the screen, because Android's main application thread is tied up sleeping and cannot redraw the screen.

因此,请确保从您所在的任何回调中返回 (onCreate()、onClick()、onListItemClick(),等),Android 应立即更新屏幕.

So, make sure that you return from whatever callback you are in (onCreate(), onClick(), onListItemClick(), etc.), and Android should update the screen momentarily.

更多推荐

使用 setText 更改文本后如何更新屏幕?

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

发布评论

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

>www.elefans.com

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