java - 为什么 Html.fromHtml 不适用于 Android 中的 String?

标签 java android firebase

enter image description here

一个字符串值来 self 的 firebase:

if (task.isSuccessful()) {
    //   binding.contentMain.noData.setVisibility(View.GONE);
    for (QueryDocumentSnapshot document : Objects.requireNonNull(task.getResult()))
        postMap.putAll(document.getData());

    try {
        if (postMap != null)
            //binding.desc1.setText(Objects.requireNonNull(postMap.get(CONTENT)).toString());
            binding.desc1.setText(Html.fromHtml(Objects.requireNonNull(postMap.get(CONTENT)).toString()));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

现在,我将告诉您 Firestore 中的 String 内部有什么内容,实际上我只是在 Firestore 集合中手动编写为 String 字段。:

This is the first text. \n\n This is a second text. \n This is the third text. \n\n Done. \n

确保上面的行只是一个字符串/firestore 字段。

在 firestore 中,我将上述值存储为 String,并获取该值并在 Android 中显示它。但它并没有采取新的路线。相反,它显示书写的\n 以及其他字符。

我尝试过使用和不使用 HTML。与\n 和\r\n。

更新:您可以在下图中看到我如何在 Firestore 中存储。 https://drive.google.com/open?id=19yry9top_W8LREw5SEaZKWYWSlnCP1io

最佳答案

在 firestore 中,您需要在 n 之前放置两个 \ 来添加新行。

如果您要从用户处获取数据或从文件中读取数据,请在存储数据之前添加此行:

str.replaceAll("\\n", "\n");

关于java - 为什么 Html.fromHtml 不适用于 Android 中的 String?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62144628/

相关文章:

java - Apache FTP 服务器 : MVN Build Failed on Java 8 (SSL)

java - JUnit 显示 0.4 秒,Infinitest 4 毫秒测试的慢速测试警告

java - 将字节数组转换为串行通信中的实际字符串

Android:隐藏配对对话框

firebase 相当于 sql where in ()

ios - 从 Firebase 控制台发送静默推送通知

java - 在 Java 中强制转换为 String 或使用 String.valueOf()

android - 包 android.support.v4.animation 不存在 导入 android.support.v4.animation.AnimatorCompatHelper;

android - 使用 Cordova 在 Android 中记录并获取音量以生成波形

dns - Firebase 托管自定义域 - 通过 "Go Live"方法更新 DNS 记录后站点不是 "CNAME records"