本文介绍了跨度字符串的 getSpans 返回跨度乱序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I use the following code to sift through a Spanned String saving all bold text as a string in an array:

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()]);

然而,我在数组中收到的字符串是乱序的.例如:

However, the Strings I receive back in the array are out of order. For example:

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

Sentence might be (CAPS represent bold text):

storyText = "This ADJECTIVE NOUN is VERB"

我返回的数组是:名词、动词、形容词"的顺序.应该是:形容词,名词,动词"

The array I'd get back would be: "Noun, Verb, Adjective" in that order. It should be: "Adjective, Noun, Verb"

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

Any insight on why this might be happening?

推荐答案

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

Use int nextSpanTransition(int start, int limit, Class kind) to iterate over your spans. This way you get the reading order.

这篇关于跨度字符串的 getSpans 返回跨度乱序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-02 02:06