首页 > 解决方案 > 选定文本的非粗体部分不起作用

问题描述

我试图找到一个答案,将文本的选定部分从其他非粗体文本中展开,但它正在将行中的整个文本展开,如图所示。我多次发布了这个问题,但没有人可以为我提供正确的答案。我正在取消“我正在使用”这句话,但是当点击它时,它会取消整行。请帮忙。

在此处输入图像描述

这是我正在使用的代码。

private void boldText(){
  int selectionStartb = texto.getSelectionStart();
  int selectionEndb = texto.getSelectionEnd();
  if (selectionStartb > selectionEndb) {
      int temp = selectionEndb;
      selectionEndb = selectionStartb;
      selectionStartb = temp;
  }
  if (selectionEndb > selectionStartb) {
      Spannable str = texto.getText();
      boolean BL = false;
      StyleSpan[] styleSpans;
      styleSpans = str.getSpans(selectionStartb, selectionEndb, StyleSpan.class);
      // If the selected text-part already has BOLD style on it, then
      // we need to disable it
      for (int i = 0; i < styleSpans.length; i++) {
          if (styleSpans[i].getStyle() == android.graphics.Typeface.BOLD) {
              str.removeSpan(styleSpans[i]);
              BL = true;
          }
      }
      // Else we set BOLD style on it
      if (!BL) {
          str.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), selectionStartb, selectionEndb,
                  Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
      }

  }

}

标签: javascriptjavaandroideclipseandroid-studio

解决方案


推荐阅读