EditText上的setSpan()删除其他范围 [英] setSpan() on EditText deletes other spans
问题描述
我有一个bold
按钮,当按下该按钮时,该按钮应该用于更改EditText
field
中所选文本的字体.除了一个例外,所有这些都可以完美运行.如果在EditText
的其他地方有bold
字体,并且我尝试创建一个新的bold
部分,则将原始部分更改回normal
.例如,以下字符串:
I have a bold
button that, when pressed, is supposed to change the typeface of the selected text in the EditText
field
. This all works perfectly, with one exception. If there is bold
typeface elsewhere in the EditText
, and I try to create a new bold
section, the original section is changed back to normal
. For example, this string:
粗体,而不是粗体.
如果我突出显示任何普通单词并按下按钮,它们的确会变成bold
,但是第一个单词会变成normal
.我在代码中看不到任何原因.
If I highlight any of the normal words and press the button, they do become bold
, but the first word becomes normal
. I can't see any reason for this in my code.
谢谢!
final StyleSpan normalStyle = new StyleSpan(Typeface.NORMAL);
final StyleSpan boldStyle = new StyleSpan(Typeface.BOLD);
boldButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int startSelection = field.getSelectionStart();
int endSelection = field.getSelectionEnd();
Editable editableText = field.getText();
StyleSpan[] selectedSpans = editableText.getSpans(startSelection, endSelection, StyleSpan.class);
boolean isAlreadyBold = false;
for (StyleSpan style : selectedSpans) {
print("The style is " + style.getStyle());
if (style.getStyle() == Typeface.BOLD) {
int styleStart = editableText.getSpanStart(style);
int styleEnd = editableText.getSpanEnd(style);
if (styleStart == startSelection && styleEnd == endSelection) {
editableText.removeSpan(style);
editableText.setSpan(normalStyle, startSelection, endSelection, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
isAlreadyBold = true;
print("Spans are equal");
} else if (styleStart < startSelection && styleEnd > endSelection) {
editableText.removeSpan(style);
editableText.setSpan(boldStyle, styleStart, startSelection, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
editableText.setSpan(boldStyle, endSelection, styleEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
editableText.setSpan(normalStyle, startSelection, endSelection, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
isAlreadyBold = true;
print("Style is larger than selection");
} else if (styleStart < startSelection && styleEnd >= startSelection) {
editableText.removeSpan(style);
editableText.setSpan(boldStyle, styleStart, startSelection, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
editableText.setSpan(normalStyle, startSelection, endSelection, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
isAlreadyBold = true;
print("Style begins before selection");
} else if (endSelection < styleEnd && endSelection >= styleStart) {
editableText.removeSpan(style);
editableText.setSpan(boldStyle, endSelection, styleEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
editableText.setSpan(normalStyle, startSelection, endSelection, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
isAlreadyBold = true;
print("Style ends after selection");
}
}
}
if (!isAlreadyBold) {
editableText.setSpan(boldStyle, startSelection, endSelection, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
}
}
});
注意:
-print
是我添加的实用程序日志
Note:
- print
is a utility log that I added
推荐答案
我在代码中看不到任何原因.
I can't see any reason for this in my code.
为每个跨区域创建一个新的StyleSpan
.请勿重用已经使用的StyleSpan
实例.
Create a new StyleSpan
for each spanned area. Do not reuse StyleSpan
instances that are already in use.
这篇关于EditText上的setSpan()删除其他范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!