问题描述
我正在尝试粗体和unbold,使文本斜体和单位,但是当我更改文本的选择时,特别是当我的文本是粗体和斜体时,我收到错误。我使用相同的代码用于Italic stylic和下面的粗体。获得spanStart和spanEnd位置可能有问题,但我无法理解。这是我的代码
Hi, I am trying to bold and unbold, make text italic and unitalic but I get an error when I change the selection of my text especially when my text is bold and italic. I am using the same code for Italic stylic as for bold given below. Maybe something wrong with getting the spanStart and spanEnd position but I can't figure it out. Here is my code
private void boldText2(){
int typeface = Typeface.NORMAL;
Spannable str = texto.getText();
int selStart = texto.getSelectionStart();
int selEnd = texto.getSelectionEnd();
StyleSpan[] styleSpans = texto.getText().getSpans(selStart, selEnd, StyleSpan.class);
for (StyleSpan styleSpan : styleSpans) {
int oldStyle = styleSpan.getStyle();
typeface = oldStyle;
int spanStart = str.getSpanStart(styleSpan);
int spanEnd = str.getSpanEnd(styleSpan);
if (oldStyle == Typeface.BOLD_ITALIC) {
str.setSpan(new StyleSpan(Typeface.ITALIC), spanStart, spanEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
Log.d("TAG", "selStart: " + selStart + " selEnd: " + selEnd + " spanStart: " + spanStart + " spanEnd: " + spanEnd);
}
if(oldStyle == Typeface.BOLD) {
str.removeSpan(styleSpan);
Log.d("TAG", "selStart: " + selStart + " selEnd: " + selEnd + " spanStart: " + spanStart + " spanEnd: " + spanEnd);
}
if (spanStart < texto.getSelectionStart()) {
str.setSpan(new StyleSpan(oldStyle), spanStart, selStart, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
Log.d("TAG", "spanStart < texto.getSelectionStart");
Log.d("TAG", "selStart: " + selStart + " selEnd: " + selEnd + " spanStart: " + spanStart + " spanEnd: " + spanEnd);
}
if (spanEnd > texto.getSelectionEnd()) {
str.setSpan(new StyleSpan(oldStyle), selEnd, spanEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
Log.d("TAG", "spanEnd > texto.getSelectionEnd()");
Log.d("TAG", "selStart: " + selStart + " selEnd: " + selEnd + " spanStart: " + spanStart + " spanEnd: " + spanEnd);
}
}
if (typeface != Typeface.BOLD) {
Log.d("TAG", "Bolding section entered");
if (styleSpans.length > 0) {
int lastSpanEnd = 0;
for (StyleSpan styleSpan : styleSpans) {
int oldStyle = styleSpan.getStyle();
int spanStart = str.getSpanStart(styleSpan);
int spanEnd = str.getSpanEnd(styleSpan);
if (spanStart > lastSpanEnd) {
str.setSpan(new StyleSpan(Typeface.BOLD), lastSpanEnd, spanStart, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
Log.d("TAG", "spanStart > lastSpanEnd and is bolded 1st if condition");
Log.d("TAG", "selStart: " + selStart + " selEnd: " + selEnd + " spanStart: " + spanStart + " spanEnd: " + spanEnd);
}
lastSpanEnd = spanEnd;
str.removeSpan(styleSpan);
if (spanStart < texto.getSelectionStart()) {
str.setSpan(new StyleSpan(oldStyle), spanStart, selStart, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
Log.d("TAG", "spanStart < texto.getSelectionStart()");
Log.d("TAG", "selStart: " + selStart + " selEnd: " + selEnd + " spanStart: " + spanStart + " spanEnd: " + spanEnd);
}
if (spanEnd > texto.getSelectionEnd()) {
str.setSpan(new StyleSpan(oldStyle), selEnd, spanEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
Log.d("TAG", "spanEnd > texto.getSelectionEnd()");
Log.d("TAG", "selStart: " + selStart + " selEnd: " + selEnd + " spanStart: " + spanStart + " spanEnd: " + spanEnd);
}
if (oldStyle == Typeface.ITALIC) {
str.setSpan(new StyleSpan(Typeface.BOLD_ITALIC), spanStart, spanEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
Log.d("TAG", "Bold and Italic");
Log.d("TAG", "selStart: " + selStart + " selEnd: " + selEnd + "spanStart: " + spanStart + " spanEnd: " + spanEnd);
}
}
if (selEnd != lastSpanEnd) {
str.setSpan(new StyleSpan(Typeface.BOLD), selStart, selEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
Log.d("TAG", "selEnd != lastSpanEnd");
Log.d("TAG", "selStart: " + selStart + " selEnd: " + selEnd);
}
}
else {
str.setSpan(new StyleSpan(Typeface.BOLD), selStart, selEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
Log.d("TAG", "Last else Bold");
Log.d("TAG", "selStart: " + selStart + " selEnd: " + selEnd);
}
}
}
这是我得到的错误
here is the error I get
java.lang.IndexOutOfBoundsException: setSpan (-1 ... 14) starts before 0
我尝试了什么:
另外粗体和斜体的工作正常但是当我的文字变得粗体和斜体时我移动了我的选择光标然后它是undbold和unitalic不工作并给我这个错误。
What I have tried:
Separately the bold and italic works fine but when my text become bold and italic and I move my selection cursor then it the undbold and unitalic is not working and give me this error.
推荐答案
这篇关于我在样式和格式化编辑文本时遇到错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!