EditText上的setSpan()删除其他跨度

时间:2015-12-19 21:05:28

标签: android android-edittext spannable

我有一个bold按钮,按下后,应该会更改EditText field中所选文字的字体。这一切都很完美,但有一个例外。如果bold中的其他位置有EditText字体,并且我尝试创建新的bold部分,则原始部分将更改回normal。例如,这个字符串:

大胆不大胆更粗体。

如果我突出显示任何常规字词并按下按钮,它们会变为bold,但第一个字会变为normal。我在代码中看不出任何理由。

谢谢!

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是我添加的实用程序日志

1 个答案:

答案 0 :(得分:3)

  

我的代码中无法找到任何理由。

为每个跨区域创建一个新的StyleSpan。不要重用已经在使用的StyleSpan个实例。