简体   繁体   English

如何在Android中刷新多行文本视图中的文本

[英]How to refresh the text in a multiline textview in android

I have a textview on the android client to show the status of the server. 我在android客户端上有一个textview来显示服务器的状态。 The status information is append to the textview as 状态信息将附加为

logTextView = FindViewById (Resource.Id.LOGtextView);
logTextView.SetText ("Client log:\n", TextView.BufferType.Normal);
logTextView.Append(string.Format("Socket connected to 172.27.27.1\n"));
logTextView.Append(string.Format ("Start send image to server\n"));
logTextView.Append(string.Format ("Successfully send the image to server\n"));
logTextView.Append(string.Format ("Successfully receive the image from server\n"));

So it should always add a new line when server status changes. 因此,当服务器状态更改时,它应始终添加新行。 However, the new lines doesn't show up immediatelly but all pop up at the end show when the processes are finished. 但是,新行不会立即显示,而是在流程结束时全部弹出。 I have set the android:singleLine="false" . 我已经设置了android:singleLine="false"

  • Can anyone help me change it into a dynamic show TextView? 谁能帮助我将其更改为动态显示TextView?
  • Or how to refesh the text in a textview every 1 second? 或者如何每隔1秒钟刷新文本视图中的文本?

You can update the textview dynamically by using timer. 您可以使用计时器动态更新textview。

    private Timer timer = new Timer();
    private TimerTask timerTask;
    timerTask = new TimerTask() {
      @Override
      public void run() {
               //refresh your textview
      }
    };
    timer.schedule(timerTask, 0, 10000);

Cancel it via timer.cancel(). 通过timer.cancel()取消它。 In your run() method you could use runOnUiThread(); 在您的run()方法中,您可以使用runOnUiThread();。

Instead of Appending a string to the text view, append the the status to your string and set it as text to your textView. 无需将字符串附加到文本视图,而是将状态附加到字符串并将其设置为文本到textView。

logTextView = FindViewById (Resource.Id.LOGtextView);

String status = string.Format("Client log:\n");
logTextView.SetText(status);

// Server status updated
status.concat(string.Format("Socket connected to 172.27.27.1\n"))
logTextView.SetText(status);

// so on

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

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