3

以下代码适用于我在 2.3 和 6.0.1 之间尝试的所有内容,但在 7.0/Emulator 和 7.1.1/Nexus 6P 中失败

SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder();
for (int i=0;i<5;++i) {
    int p = spannableStringBuilder.length();
    spannableStringBuilder.append("aAAAAa\n");
    spannableStringBuilder.setSpan(new StyleSpan(Typeface.ITALIC), p+1, p+5, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}

boolean pass = true;
StyleSpan[] spans = spannableStringBuilder.getSpans(0, spannableStringBuilder.length(), StyleSpan.class);
int p = 0;
for (StyleSpan span : spans) {
    int start = spannableStringBuilder.getSpanStart(span);
    if (start<p) {
        pass = false;
        break;
    }
    p = start;
}
4

1 回答 1

3

我正在使用的当前解决方法

Arrays.sort(spans, new Comparator<ClickableSpan>() {
    @Override
    public int compare(ClickableSpan o1, ClickableSpan o2) {
        return s.getSpanStart(o1)-s.getSpanStart(o2);
    }
});
于 2016-12-09T02:28:43.173 回答