罢工html标记未在具有TextFormatted的EditText中呈现 [英] Strike html tag not rendering in EditText with TextFormatted

查看:97
本文介绍了罢工html标记未在具有TextFormatted的EditText中呈现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在EditText控件中呈现html字符串.粗体,斜体和下划线html可以正确呈现,但是删除线仅被忽略.

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.

这里是EditText控件,没什么花哨的:

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

这是在EditText控件中设置html的代码:

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?

推荐答案

这是我解决问题的方法.我创建了一个ITagHandler的实现:

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 {
    }

可以这样称呼:

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

这是它的外观:

这篇关于罢工html标记未在具有TextFormatted的EditText中呈现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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