【问题标题】:How to dynamically append a new line to a multiline textview如何动态地将新行附加到多行文本视图
【发布时间】:2015-04-30 08:24:38
【问题描述】:

我有一个多行文本视图需要显示服务器的状态。因此,当服务器状态发生变化时,它应该始终添加一个新行。但是,当我使用 mTextview.Text=string.Format("connected\n");mTextview.SetText("...") 时,新行不会立即显示,而是显示所有进程何时完成。谁能帮我把它改成自动显示的TextView?谢了

logTextView.Text = string.Format ("Client log:\n");
.......
logTextView.Text=string.Format("Socket connected to 172.27.27.1\n");
.......
logTextView.Text = logTextView.Text+string.Format ("Start send image to server\n");
.......

【问题讨论】:

  • 你提到“当所有进程都完成时”它就会显示出来,进程和 logTextView 是同步进程吗?
  • 如果您想将add 文本发送到TextView,那么最好使用textView.append("\nmy new text");。但是由于您想将它与套接字一起使用,并且您的套接字语句将在线程或 AsyncTask 中,因此您不能在那里使用这些“更改 gui 语句”。你的应用会崩溃。
  • @JamieRees 我的意思是在套接字关闭之后。一切都完成了,然后文本出现了。这绝对不是我想要的。我用textview.append(" abc") 但还是一样...

标签: java c# android xamarin


【解决方案1】:

你应该使用:

YourTextView.setText("Connected\n");

并添加多行使用:

YourTextView.append("another line\n");

【讨论】:

  • 您是否不小心为多行文本视图设置了最大线?
  • 不,我设置了android:singleLine="false"
【解决方案2】:

使用该日志进程创建一个新线程,并在某些事件发生后立即调用它。

例如:

class LogProcess implements Runnable {

    private String message;

    private TextView textView;

    public LogProcess(TextView textView, String message) {
        this.textView = textView;
        this.message = message;
    }

    @Override
    public void run() {
        textView.append(message);
    }

}

然后调用它:

Thread logProcess = new Thread(new LogProcess(logTextView, message));
logProcess .start();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多