Settext 导致文本不正常闪烁 [英] Settext causing text to flicker irratically

查看:23
本文介绍了Settext 导致文本不正常闪烁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个应用程序,用于在 Arduino UNO R3 和 android 平板电脑之间建立 USB 通信.Arduino 板正在正确发送数据,它甚至被平板电脑正确接收,当试图显示时,文本确实被打印出来,但有相当连续的闪烁.

I'm making an app which establishes USB communication between an Arduino UNO R3 and an android tablet. Arduino board is sending data correctly and it is even being received by tablet correctly and when tried to display, the text does get printed but with a rather continuous flicker.

class MyThread extends Thread
{
    @Override
    public void run()
    {
        mCallback = new UsbSerialInterface.UsbReadCallback()
        { //Defining a Callback which triggers whenever data is read.
            @Override
            public void onReceivedData(byte[] arg0) //data received in bytes
            {
                String data = null;

                try
                {

                    data = new String(arg0, "UTF-8");
                    handler.post(new newthread(data));                            

                }
                catch (UnsupportedEncodingException e)
                {
                    e.printStackTrace();
                }
            }
        };
    }
}
class newthread implements Runnable
{
    String str1;        

    public newthread(String STR1)
    {            
        str1 = STR1;
    }
    @Override
    public void run()
    {

        DoseRateDisplay = (TextView) findViewById(R.id.DoseRateDisplay);
        if(str1.contains("L"))
        { tv6.append("Health OK"); }
        else
        {
           DoseRateDisplay.settext(str1);
        }
    }
}

我认为闪烁的原因可能是数据传入太快.使用 Thread.sleep 没有帮助.这个问题的可能解决方案是什么?此外,使用 append 而不是 settext 不会导致任何闪烁问题,但随后数据会附加到 textview.

I think the reason for flicker can be that the data is incoming too fast. Using Thread.sleep does not help. What can be the possible solution to this problem ? Also, using append instead of settext doesn't cause any flickering problems, but then data gets appended to textview.

推荐答案

来自我的评论:尝试检查收到的文本和 TextView 中已有的文本是否相等:

From my comment: try to check if the text received and the text already in the TextView are equal:

if(!DoseRateDisplay.getText().toString().equals(str1)) {
    DoseRateDisplay.settext(str1);
}

这篇关于Settext 导致文本不正常闪烁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆