为什么Html.fromHtml在Android中不适用于String? [英] Why doesn't Html.fromHtml not working for String in Android?

查看:52
本文介绍了为什么Html.fromHtml在Android中不适用于String?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个字符串值来自我的firebase:

One String value is coming from my 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字段.

Now, I'll tell you what is coming inside String from Firestore, Actually I only manually written as a String field inside Firestore collection.:

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

确保上面的行只是一个String/firestore字段.

Make sure that above line is just one String/firestore field.

在firestore中,我将上述值存储为 String ,然后获取该值并将其显示在Android中.但这并没有换行.而是显示\ n和其他字符.

In firestore, I stored the above value as a String, and fetching that value and showing it in Android. But it is not taking a new line. Instead, it is showing written \n along with other characters.

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

I tried with and without HTML. with \n and \r\n.

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

UPDATE: You can see below image how I stored in Firestore. https://drive.google.com/open?id=19yry9top_W8LREw5SEaZKWYWSlnCP1io

推荐答案

您的字符串是

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

这很奇怪.

1)是否要在句号结束后下一行?

1) Do you want next line after a full stop?

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

2) Can you edit or format the string at the time of saving?

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

If your case is 1 then what you can do is

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

Remove all the /n character and replace with "".

字符串stringWithoutNewLine = yourString.replaceAll("\ n",")

然后它会看起来像这样

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

现在,删除字符串中的所有.",并将其替换为新行.

Now remove all the ". " in the string and replace it with new line.

字符串formattedString = yourString.replaceAll(.
,System.lineSeparator())

您可以将它们链接成一行,这将起作用,因为 replaceAll 返回一个新的 String

You can chain these in one line and it will work because replaceAll returns a new 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();
    }
}

这篇关于为什么Html.fromHtml在Android中不适用于String?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆