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 not bold more bold.
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.
Thanks!
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);
}
}
});
Note:
- print
is a utility log that I added
I can't see any reason for this in my code.
Create a new StyleSpan
for each spanned area. Do not reuse StyleSpan
instances that are already in use.
来源:https://stackoverflow.com/questions/34375401/setspan-on-edittext-deletes-other-spans