减少我的选择结束指针时的跨度错误

时间:2018-11-11 06:04:10

标签: java spannablestring

我能够对编辑文本进行粗体显示/粗体显示/斜体显示,但是在一种情况下,当我将结束选择指针指向开始选择指针,然后单击斜体或粗体按钮时,它将引发错误并使我的应用程序崩溃。我花了整整一整天,但找不到原因和解决方案。请帮忙。

我的斜体编码与粗体字相同

 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);
      }
  }

  }

给我以下错误

  

java.lang.IndexOutOfBoundsException:setSpan(-1 ... 0)在0之前开始

此代码

 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);
               }

0 个答案:

没有答案