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

时间:2018-01-06 20:53:08

标签: android html textview spannablestring spannable

请阅读底部的编辑更新。

我对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>

活动代码如下:

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);

这给了我错误的输出。注意输出中缺少字符串的整个第一行,整个字符串显示为粗体。

enter image description here

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=,则它会开始正常运行。这是一个错误吗?

1 个答案:

答案 0 :(得分:1)

您的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/">

如果可以,那么你不需要在这里转义引号,因为它们没有被转义。

所以,我会删除逃生反斜杠,以简化你的生活。