繁体   English   中英

Android TextView没有完全更新

[英]Android TextView not fully updating

我有一个使用runnable更新textview的线程:

// runnable to allow updating the UI from the thread
Runnable updateTextView = new Runnable() {

    public void run() {
        mTextView.setText(mDisplayedText);
        mTextView.invalidate();
    }
};

但是,文本未正确更新。 它适用于前几次写入,然后只写入文本的一半,留下前一文本的后半部分。

转动屏幕会使其刷新并正确绘制。

textview是多行的,我给它写了一个字符串,其中包含行尾的\\ n字符。

上面的无效调用没有区别。

有任何想法吗?

更新:mDisplayedText在活动中声明,该活动也包含我的线程类。 在线程运行循环中,我调用:

mDisplayedText = getText()
runOnUiThread(updateTextView);

该循环包含一个100ms的睡眠,但它只在文本发生变化时写入文本,因此实际上它会少于一秒钟

回答:

这有点令人尴尬。 问题出在其他代码中。

我在同一个数据包中收到UDP套接字,并将packet.getData读入一个新字符串。 这是将整个数据包复制到字符串中,而不仅仅是在该消息中收到的字节。 第二个问题是我每次都需要调用packet.setLength来设置整个数据包的可用长度。

谢谢你的回答!

你只能在UI线程上更新你的UI,把你的setText()方法放在其中

runOnUiThread(new Runnable() {
   @Override
   public void run() {
      btn.setText(someValue);
   }
});

Runnable是一个界面。 可以把它想象成一组准备好执行的命令。 您可以使用处理程序来处理代码:

Handler textViewHandler = new Handler();

// runnable to allow updating the UI from the thread
Runnable updateTextView = new Runnable() {

    public void run() {
        mTextView.setText(mDisplayedText);
    }
};

textViewHandler.post(updateTextView);

另外,请考虑@ Karakuri的建议。

暂无
暂无

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

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