Settext导致文本不规则地闪烁 [英] Settext causing text to flicker irratically

查看:161
本文介绍了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天全站免登陆