setSpan() on EditText deletes other spans

帅比萌擦擦* 提交于 2019-12-22 14:47:26

问题


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


回答1:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!