跨度字符串的 getSpans 返回跨度乱序?

san*_*nic 5 html string android arraylist spanned

我使用以下代码筛选跨区字符串,将所有粗体文本保存为数组中的字符串:

StyleSpan[] spans = storyText.getSpans(0,
        storyText.length(), StyleSpan.class);
List<String> boldedWords = new ArrayList<String>();
for (StyleSpan span : spans) {
    if (span.getStyle() == Typeface.BOLD) {
        //start
        int s = storyText
                .getSpanStart(span);
        //end
        int e = storyText.getSpanEnd(span);
        boldedWords.add(storyText.subSequence(
                s, e).toString());
    }
}

String[] array = boldedWords
        .toArray(new String[boldedWords.size()]);
Run Code Online (Sandbox Code Playgroud)

但是,我在数组中收到的字符串是乱序的。例如:

句子可能是(大写代表粗体文本):

storyText = "This ADJECTIVE NOUN is VERB"
Run Code Online (Sandbox Code Playgroud)

我要返回的数组是:按“名词、动词、形容词”的顺序排列。应该是:“形容词,名词,动词”

关于为什么会发生这种情况的任何见解?

ste*_*han 3

用于int nextSpanTransition(int start, int limit, Class kind)迭代您的跨度。这样你就可以得到阅读顺序。