【问题标题】:Android: New line character from mysql does not work in textviewAndroid:来自mysql的换行符在textview中不起作用
【发布时间】:2014-12-22 20:31:28
【问题描述】:

我在 mysql db(长文本类型)中保存了一些文本,其中包含换行符,例如:

Hello World
This is a test
Thanks

我通过 php 在 db 中发送值,如下所示:

$message = 'Hello World' . "\n";
$message .= 'This is a test' . "\n";
$message .= 'Thanks;

我可以看到换行符保存在 db 中并且显示正常。

但是在 TextView 中我看到所有文本都加入了,没有换行符。这是 TextView 的显示方式:

Hello WorldThis is a testThanks

我尝试过的:

  • 设置android:singleLine="false"
  • msg.replaceAll("\n", System.getProperty("line.separator"))

仅供参考,如果我在 android 应用程序本身中键入包含换行符的相同消息,则 TextView 会正确显示换行符,但它只是没有从 mysql db 获取换行符:(

感谢您的帮助

【问题讨论】:

    标签: android mysql newline


    【解决方案1】:

    使用

    msg.replaceAll("\n","<br />");
    

    然后在文本视图上使用

    myTextView.setText(Html.fromHtml(msg));
    

    【讨论】:

      【解决方案2】:

      将\n替换为“html br标签”和

      使用这个方法yourTextView.setText(Html.fromHtml(text));

      【讨论】:

      • 我想在 android TextView 而不是 php 应用程序中显示它
      • 但是TextView上没有setHTML方法
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-22
      • 2016-02-22
      • 2011-06-15
      • 1970-01-01
      • 1970-01-01
      • 2021-10-17
      相关资源
      最近更新 更多