gpt4 book ai didi

android - 合并 spannable 对象时出错

转载 作者:太空宇宙 更新时间:2023-11-03 11:32:34 28 4
gpt4 key购买 nike

我想合并 3 个 spannable 对象。此代码工作正常:

Spannable s1 = new SpannableStringBuilder("bold");
s1.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, s1.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
Spannable s2 = new SpannableStringBuilder("not");
Spannable s3 = new SpannableStringBuilder("BOLD");
s3.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, s3.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
finishSpan = (Spanned) TextUtils.concat(s1,s2);
finishSpan = (Spanned) TextUtils.concat(finishSpan,s3);

///////////////////////////////////////////////////

finishSpan = (Spanned) TextUtils.concat(s1,s2,s3);

我有相同的代码,但是当我合并 3 个对象时,结果是错误的。我已经检查了某些元素的类型是否正确。
beginningOfModifiedSpannable 为粗体,selectionSpannable 为普通,endOfModifiedSpannable 为粗体但是他们的合并是错误的。只有结果字符串的最后部分是粗体。为什么会发生??我在上面有相同的代码并且运行良好!

  Spannable str = contentText.getText();
Spannable selectionSpannable = new SpannableStringBuilder(str, selectionStart, selectionEnd);
StyleSpan[] ss = selectionSpannable.getSpans(0, selectionSpannable.length(), StyleSpan.class);

boolean exists = false;
for (int i = 0; i < ss.length; i++) {
if (ss[i].getStyle() == android.graphics.Typeface.BOLD){
selectionSpannable.removeSpan(ss[i]);
exists = true;
}
}

if (!exists){
str.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), selectionStart, selectionEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
else {

Spannable endOfModifiedSpannable = new SpannableStringBuilder(str, selectionEnd, contentText.getText().length());
Spannable beginningOfModifiedSpannable = new SpannableStringBuilder(str, 0, selectionStart);
Spanned finishSpan = null;

if(beginningOfModifiedSpannable.length() > 0) {

if(endOfModifiedSpannable.length() > 0) {
finishSpan = (Spanned) TextUtils.concat(beginningOfModifiedSpannable,selectionSpannable);
finishSpan = (Spanned) TextUtils.concat(finishSpan,endOfModifiedSpannable);
}
else {
finishSpan = (Spanned) TextUtils.concat(beginningOfModifiedSpannable,selectionSpannable);
}

}
else {
if(endOfModifiedSpannable.length() > 0) {
finishSpan = (Spanned) TextUtils.concat(selectionSpannable,endOfModifiedSpannable);
}
else {
finishSpan = selectionSpannable;
}
}
contentText.setText(finishSpan);

我再说一遍,我已经检查过 3 个部分都是真实的。例如。我有 3 个部分:你好你好ld

我注意到第一次合并是正确的:

finishSpan = (Spanned) TextUtils.concat(beginningOfModifiedSpannable,selectionSpannable);

看起来像Hello,wor

但是第二个是错误的

finishSpan = (Spanned) TextUtils.concat(finishSpan,endOfModifiedSpannable);

或者这个合并是错误的:

finishSpan = (Spanned) TextUtils.concat(finishSpan,selectionSpannable,endOfModifiedSpannable);

错误字符串的结果是:你好,我rld

但真正的结果一定是这样的:

你好你好,世界

废话!

最佳答案

问题是您很可能对第一个和第三个部分使用相同的 CharacterStyle 实例。每次向 Spannable 对象添加样式时都需要使用新实例。

例如

    SpannableStringBuilder ssb = new SpannableStringBuilder("Hello,world");
CharacterStyle c = new StyleSpan(Typeface.BOLD);
ssb.setSpan(c, 0, 3, 0);
ssb.setSpan(c, 9, 11, 0);

这将导致 Hello,World

要获得正确的结果,您必须执行以下操作:

    SpannableStringBuilder ssb = new SpannableStringBuilder("Hello,world");
CharacterStyle c = new StyleSpan(Typeface.BOLD);
CharacterStyle c2 = new StyleSpan(Typeface.BOLD);
ssb.setSpan(c, 0, 3, 0);
ssb.setSpan(c2, 9, 11, 0);

关于android - 合并 spannable 对象时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4669321/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com