奇怪的 TagHandler 行为检测开始和结束标签 [英] Weird TagHandler behavior detecting opening and closing tags

查看:22
本文介绍了奇怪的 TagHandler 行为检测开始和结束标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 TextView 显示带有自定义标签的字符串文本:

I am trying to use TextView to display a String text with custom tags:

字符串:

"<articlelink>text1</articlelink> padding<articlelink>text2</articlelink>"

其中 articlelink 是自定义标签.我使用自定义的 HTML.TagHandler 来处理标签:

Where articlelink is a custom tag. I use a customized HTML.TagHandler to handle the tags:

private class MyTagHandler implements Html.TagHandler {

    private int startIndex = 0;
    private int endIndex = 0;

    @Override
    public void handleTag(boolean opening, String tag, Editable output, XMLReader xmlReader) {
        if (tag.equals("articlelink")) {
            if (opening) {
                startIndex = output.length();
                DebugLog.d("OPEN " + startIndex);
            } else {
                endIndex = output.length();
                DebugLog.d("END " + endIndex);
                MyClickableSpan span = new MyClickableSpan();
                output.setSpan(span, startIndex, endIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }
    }
}

但是日志是:

OPEN 0
OPEN 13
END 18
END 18

但是,在字符串之前插入一个字符后,输出就是我所期望的:

However, after I insert a character before the string then the output is what I expected:

字符串:

"a<articlelink>text1</articlelink> padding<articlelink>text2</articlelink>"

输出:

OPEN 1
END 6
OPEN 14
END 19

这里发生了什么?这是一个错误还是我误用了它?

What happened here? Is this a bug or I misused it?

推荐答案

我通过在字符串开头添加zero width joiner"解决了这个问题

I solved this problem by adding to the beginning of the string "zero width joiner"

字符串看起来像:

"&zwj;<articlelink>text1</articlelink>padding<articlelink>text2</articlelink>"

在结果文本视图中,该符号不可见,文本看起来像原始字符串

In result textview, this symbol is not visible and text looks like the original string

这篇关于奇怪的 TagHandler 行为检测开始和结束标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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