Strike html tag not rendering in EditText with Tex

2019-05-10 19:41发布

问题:

I am attempting to render an html string within an EditText control. Bold, italic, and underline html renders correctly, however strike through is just ignored.

Here is the EditText control, nothing fancy:

<EditText
        android:id="@+id/rich_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="@style/Widget.EditText"
        android:gravity="top"
        android:inputType="textFilter|textMultiLine|textNoSuggestions"
        android:minLines="8"
        android:textStyle="normal" />

And here is the code that is setting the html in the EditText control:

var textView = view.FindViewById<EditText> (Resource.Id.rich_text);
var html = "<b>bold test</b> <u>underline test</u> <i>italic test</i> <strike>strike test 1</strike> <del>strike test 2</del> <s>strike test 3</s>";
textView.TextFormatted = Html.FromHtml (html);

Here is a screenshot of how it displays, notice how the strike through tests aren't working.

Any ideas what I'm doing wrong?

回答1:

Here is how I solved the issue. I created an implemented of ITagHandler:

public class HtmlTagHandler : Object, Html.ITagHandler {
        public void HandleTag (bool opening, string tag, IEditable output, IXMLReader xmlReader) {  
            if (tag == "strike" || tag == "s" || tag == "del") {
                var text = output as SpannableStringBuilder;
                if (opening)
                    Start (text, new Strike ());
                else
                    End (text, Class.FromType (typeof(Strike)), new StrikethroughSpan ());
            }
        }

        private static void Start (SpannableStringBuilder text, Object mark) {
            var length = text.Length ();
            text.SetSpan (mark, length, length, SpanTypes.MarkMark);
        }

        private static void End (SpannableStringBuilder text, Class kind, Object newSpan) {
            var length = text.Length ();
            var span = GetLast (text, kind);
            var where = text.GetSpanStart (span);
            text.RemoveSpan (span);
            if (where != length)
                text.SetSpan (newSpan, where, length, SpanTypes.ExclusiveExclusive);
        }

        private static Object GetLast (ISpanned text, Class kind) {
            var length = text.Length ();
            var spans = text.GetSpans (0, length, kind);
            return spans.Length > 0 ? spans.Last () : null;
        }
    }

    class Strike : Object {
    }

This can be called like so:

public static ISpanned ToHtml (this string html) {
            return Html.FromHtml (html ?? string.Empty, null, new HtmlTagHandler ());
        }

And here is how it looks:



回答2:

<strike> is not supported. I can't find an official documentation with list of all supported HTML tags. However, if you look at the source code you won't find support there.

https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/text/Html.java

What I can gather the following tags are supported:

Tags                Format
b, strong           Bold
i, em, cite, dfn    Italics
u                   Underline
sub                 Subtext
sup                 Supertext
big                 Big
small               Small
tt                  Monospace
h1 … h6             Headlines
img                 Image
font                Font face and color
blockquote          For longer quotes
a                   Link
div, p              Paragraph
br                  Linefeed