【问题标题】:How do I show data in Textview for a 2 second (android Studio)?如何在 Textview 中显示数据 2 秒(android Studio)?
【发布时间】:2020-06-23 03:19:28
【问题描述】:

我想在 Textview 中显示 2 秒的数据。所以我从 arduino 收到的数据在我使用 serialwrite 之间没有延迟。我使用 InputStream 和 DatainputStream 来接收数据。当数据在 textview 中显示时仍然有问题.它会突然出现下一个数据。有什么方法可以让它保持2秒并随机到下一个?我在 android Studio 中编写代码。 .



private void receive(byte[] data) {
        InputStream is = null;
        DataInputStream dis = null;
        try {
            is = new ByteArrayInputStream(data);
            dis = new DataInputStream(is);
            while (dis.available() > 0) {
                byte[] b = new byte[data.length];
                dis.read(b, 0, 8);
                 count++;
                String part = HexDump.dumpHexString(b).substring(HexDump.dumpHexString(b).indexOf("E7 0"), HexDump.dumpHexString(b).indexOf("0A"));

               
             if (part.contains("E7 01")) {
                   //receive_text2.setText((part));
                    show_text(receive_text2,part);


                }
                if (part.contains("E7 02")) {
                    receive_text3.setText(String.valueOf(1000*unsiged(b[5])+100*unsiged(b[6])));

                }
                if (part.contains("E7 03")) {
                    receive_text4.setText((part));

                }
                if (part.contains("E7 05")) {
                    receive_text5.setText((part));

                }
                receive_text6.setText(String.valueOf(count));

            }
                


            

        }catch (Exception e){
             receive_text2.append("");
             receive_text3.append("");
             receive_text4.append("");
             receive_text5.append("");
             receive_text6.append("");
          }
    }

【问题讨论】:

    标签: java android datainputstream timedelay


    【解决方案1】:

    您可以尝试在每次尝试设置文本值时使用 Handler 延迟 2 秒(2000 毫秒)。

    TextView textView = ...;
    textView.postDelayed(new Runnable() {
        @Override
        public void run() {
            textView.setText("some text");
        }
    }, 2000); // delay of 2 seconds before setting a text to textView
    

    【讨论】:

    • 不行。结果是延迟2秒后显示数据。不再延迟了。
    【解决方案2】:

    将要显示的所有字符串添加到 Arraylist 中

    ArrayList<String> strings = new ArrayList<>();
    

    然后做:

    Runnable job = new Runnable() {
        @Override
        public void run() {
            if (!strings.isEmpty()) {
                textView.setText(strings.get(0));
                strings.remove(0);
                textView.postDelayed(this, 2000);
            } else {
                textView.removeCallbacks(this);
            }
        }
    };
    
    textView.postDelayed(job, 2000); // delay of 2 seconds before setting a text to textView
    

    【讨论】:

      猜你喜欢
      • 2015-11-18
      • 2015-10-22
      • 2017-02-28
      • 2017-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多