android - Spanned 字符串的 getSpans 返回 Spanned 乱序?

标签 android html string 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()]);

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

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

storyText = "This ADJECTIVE NOUN is VERB"

我返回的数组将是:“名词、动词、形容词”的顺序。应该是:“形容词、名词、动词”

对为什么会发生这种情况有任何见解吗?

最佳答案

使用int nextSpanTransition(int start, int limit, Class kind)迭代你的 Spanned 。这样你就得到了阅读顺序。

关于android - Spanned 字符串的 getSpans 返回 Spanned 乱序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25097733/

相关文章:

python - 在 Python 中验证字符串值

c# - C# 中不区分大小写的比较

Android:如何垂直对齐:单选按钮顶部到多行文本?

java.lang.IllegalArgumentException - 对话框关闭

javascript - 不要在 IE 中使用 Bootstrap 显示网站

javascript - angular2 中不再存在的 routerState.parent 的替代方案

android - 在 GridView 中动态添加整行,或 Split View

android - 如何在 Dialog 中正确使用 FragmentResultListener

javascript - 滚动页面时为 div 设置动画

c++ - C++ 中的字符串 : Problems with good() and get()