【问题标题】:Why doesn't Html.fromHtml not working for String in Android?为什么 Html.fromHtml 不适用于 Android 中的 String?
【发布时间】:2020-09-20 11:40:11
【问题描述】:

一个字符串值来自我的 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

【问题讨论】:

  • 这是因为\\n 而不是\n。你需要取消它:s.replaceAll("\\\\n", "\\\n"); 或者如果你可以导入 apache commons 那么有一个静态方法String op = StringEscapeUtils.unescapeJava(s);
  • @AniketSahrawat 我到底需要在 Firesotore 字段中写些什么?
  • @AniketSahrawat 在我的情况下,它不会用 \n 替换为 \\n ;它完全符合我在收藏字段中手动编写的内容。
  • @AniketSahrawat 检查已编辑的问题。

标签: java android firebase


【解决方案1】:

你的字符串是

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

这很奇怪。

1)你想next line在句号之后吗?

2)您可以在保存时编辑或格式化字符串吗?

如果您的案例是1,那么您可以做的是

删除所有/n 字符并替换为""

String stringWithoutNewLine = yourString.replaceAll("\n","")

然后会是这个样子

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

现在删除字符串中的所有“.”并用新行替换它。

String formattedString = yourString.replaceAll(".
",System.lineSeparator())

您可以将这些链接在一行中,因为replaceAll 返回一个新的String

你可以试试这个

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());
            String string = postMap.get(CONTENT).toString();
            String finalString = string.replaceAll("\n","").replaceAll(".  ",System.lineSeparator())
            binding.desc1.setText(finalString)
    } catch (Exception e) {
        e.printStackTrace();
    }
}

【讨论】:

  • 无论我在 Firestore 中写了什么,它都即将到来。看问题,我写的。 Ans 看到里面的图片,问题是什么。
  • 我在 Firebase 中有自己的项目。无论我想要什么,我都可以手动在 Firesotore DB 中编写。而且我不想在每个句号之后换行。这取决于一些文章。在段落之后我想要/添加新行,这样,在 android 中就会自动出现空间。在问题中,我只是举了一个例子。
  • @ShefaliSingh 我建议您在将字符串保存到firebase之前对其进行格式化,然后您就可以成功地格式化字符串。否则,您将很难格式化字符串。
【解决方案2】:

使用这个方法:

  public Spanned getHtmlFormattedString(String value) {
        Spanned result = null;
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                result = Html.fromHtml(value, Html.FROM_HTML_MODE_COMPACT);
            } else {
                result = Html.fromHtml(value);
            }

        } catch (Exception ex) {
        }
        return result;
    }

在你的情况下:

binding.desc1.setText(getHtmlFormattedString(Objects.requireNonNull(postMap.get(CONTENT)).toString()));

更新答案

\n不是html标签,如果你想用Html.fromHtml处理你应该用<br/>标签替换\n

所以在你的情况下:

binding.desc1.setText(getHtmlFormattedString(Objects.requireNonNull(postMap.get(CONTENT)).toString().replaceAll("\\n","<br/>")));

【讨论】:

【解决方案3】:

在 Firestore 中,您需要在 n 之前放置两个 \s 以添加新行。

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

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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2023-04-07
  • 1970-01-01
  • 2013-08-07
  • 2014-09-20
  • 2020-01-20
  • 1970-01-01
  • 2018-02-19
  • 2023-02-24
相关资源
最近更新 更多