如果第一个引号被转义,为什么 Spannable fromHtml 会给我错误的不正确输出?它删除了第一句话

Why is Spannable fromHtml giving me wrong incorrect output if the first quote is escaped? It removes the first sentence

请阅读底部的编辑更新。

我是 Android 的新手,所以请多多包涵。我可以用一个非常简单的例子重现我的问题。

我的 strings.html 中有以下内容:

<string name="nice_html">
<![CDATA[
<div><p><strong><a href=\"https://www.reddit.com/r/nosleep/comments/3ijnt6/im_a_search_and_rescue_officer_for_the_us_forest/">Timeline</a> of my other stories separated by company.</strong></p><p><a href=\"https://redd.it/7jmvxo\">This</a> story about smashing modems from <a href=\"/u/devdevo1919\">u/devdevo1919</a> reminded of this little gem when I was a customer service representative at $SecurityCompany. Now before you say \u201cbut customer service isn\u2019t IT\u201d, let me explain the position. They <em>called</em> us CSRs, but what we really did was mostly technical support for the residential and small business security systems as well as some customer service functions. We had a separate team for the large businesses.</p></div>
]]>
</string>

我的mainactivity.xml看起来像:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.pranapps.htmltest.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:id="@+id/mylabel"
        android:layout_marginTop="25dp"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        />

</android.support.constraint.ConstraintLayout>

activity 的代码如下所示:

TextView mylabel = (TextView) findViewById(R.id.mylabel);

        String htmlstring = getString(R.string.nice_html);
        Spannable spannable = (Spannable) Html.fromHtml(htmlstring);
        System.out.println("htmlstring: "+htmlstring);
        System.out.println("spannable: "+spannable);
        mylabel.setText(spannable);

这给了我错误的输出。请注意字符串的整个第一行在输出中是如何丢失的,并且整个字符串显示为粗体。

system.out 显示 Spannable 是导致问题的原因,并且出于某种原因它正在修剪整个第一行。谁能帮我看看为什么会这样?

01-06 15:47:12.470 14719-14719/com.XXXXXX.htmltest I/System.out: htmlstring: <div><p><strong><a href="https://www.reddit.com/r/nosleep/comments/3ijnt6/im_a_search_and_rescue_officer_for_the_us_forest/>Timeline</a> of my other stories separated by company.</strong></p><p><a href="https://redd.it/7jmvxo">This</a> story about smashing modems from <a href="/u/devdevo1919">u/devdevo1919</a> reminded of this little gem when I was a customer service representative at $SecurityCompany. Now before you say “but customer service isn’t IT”, let me explain the position. They <em>called</em> us CSRs, but what we really did was mostly technical support for the residential and small business security systems as well as some customer service functions. We had a separate team for the large businesses.</p></div>


01-06 15:47:12.470 14719-14719/com.XXXXXX.htmltest I/System.out: spannable: This story about smashing modems from u/devdevo1919 reminded of this little gem when I was a customer service representative at $SecurityCompany. Now before you say “but customer service isn’t IT”, let me explain the position. They called us CSRs, but what we really did was mostly technical support for the residential and small business security systems as well as some customer service functions. We had a separate team for the large businesses.

编辑: 我想通了,但我很好奇这是否是一个错误。如果我只删除 href= 标记之后的第一个 \,它就会开始正常工作。这是一个错误吗?

您的 HTML 在 CDATA 块中。 AFAIK,您不需要在那里转义引号。但是,如果你这样做,显然你需要始终如一地这样做。

你的第二个 <a> 是:

<a href=\"https://redd.it/7jmvxo\">

请注意,两个引号都用反斜杠转义了。

您的第一个 <a> 是:

<a href=\"https://www.reddit.com/r/nosleep/comments/3ijnt6/im_a_search_and_rescue_officer_for_the_us_forest/">

请注意,您转义了左引号,但没有转义右引号。

根据您的评论,您修复的 <a> 是:

<a href="https://www.reddit.com/r/nosleep/comments/3ijnt6/im_a_search_and_rescue_officer_for_the_us_forest/">

如果可行,那么您无需在此处转义引号,因为引号不会转义。

所以,我会删除转义反斜杠,以简化您的生活。