Android TagHandler 对标准标签没有影响 [英] Android TagHandler no effect on standard tags

查看:41
本文介绍了Android TagHandler 对标准标签没有影响的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 TextView 来显示 HTML 字符串,例如:

I am using an TextView to display an HTML string such as:

"测试 HTML link1 link2"

"Test HTML < a href=\"www.type1.com\">link1< /a> < a href=\"www.type2.com\">link2< /a>"

如您所见,我需要处理两种不同类型的标签,因此我需要能够处理两种不同类型的标签并读取 href 属性.

As you see, there are two different kinds of tags that I need to handle, so I need to be able to handle the two different kinds of tags and read the href attribute.

我尝试使用 Html.TagHandler:

I tried using Html.TagHandler:

private class MyTagHandler implements Html.TagHandler {
    @Override
    public void handleTag(boolean opening, String tag, Editable output, XMLReader xmlReader) {
        Toast.makeText(getContext(), tag, Toast.LENGTH_LONG).show();
    }
}

然而,在

上没有调用 handleTag.> 标签.我做了测试,发现它只对自定义标签有影响.是否也可以处理标准标签?

However the handleTag is not called on the < a > tag. I did tests and figure out it only has effect on the customized tags. Is it possible to also handle the stardard tags?

推荐答案

Html.TagHandler 自定义实现的目标是提供对 android 框架未处理的标签的处理.因此,为了做您想做的事情,一种解决方法是将您想要处理的所有标签替换为您知道框架不会处理的另一个标签,因此它将进入您的实现.例如,您可以使用这样的方法来准备您的 html:

The goal of a Html.TagHandler custom implementation is to provide handling of tags that are not handled by the android framework. So in order to do what you want, one workaround is to replace all the tags that you want to handle with another tag that you know the framework won't handle, so it will enter into your implementation. For example, you could do a method like this one to prepare your html:

public string prepareHTMLForTagHandling(string htmlSource)
    {
        if (htmlSource == null || htmlSource == "")
            return null;

        return htmlSource.replace("<a", "<acustomlink")
                         .replace("</a>", "<acustomlink>");
    }

然后像这样使用它:

Html.fromHtml(prepareHTMLForTagHandling(myHtml), null, myHtmlCustomTagHandler);

最后,在您的自定义标签处理程序实现中,您将acustomlink"作为标签而不是a"来处理.

Finally, in your custom tag handler implementation you handle "acustomlink" as a tag instead of "a".

希望有帮助.

这篇关于Android TagHandler 对标准标签没有影响的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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