editable.getSpans返回一个StyleSpan,即使屏幕上没有任何样式呈现

时间:2016-12-26 17:33:55

标签: android android-edittext android-textwatcher

我正在写一个RichTextEditor类,它有一个内部RichTextEditorTextWatcher类。我发现屏幕上显示的内容与我在e.getSpans内拨打beforeTextChanged时得到的内容存在差异。

在屏幕上,我看到屏幕上的文字(在我的情况下,单个字符)没有应用任何样式,但e.getSpans()调用实际上说我应用了粗体样式。

这是一个已知的Android错误吗?

public class RichTextEditor extends AppCompatEditText  
{

    // other code not shown

    public class RichTextEditorTextWatcher implements TextWatcher
    {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after)
        {
            if (after == 0) //deletion occurred
            {
                isDeletion = true;
                Editable e = RichTextEditor.this.getText();

                /** The next line is the problematic line!
                  * this.prevStyles returns a StyleSpan (bold) even when I don't see it on the screen for that character.
                  */
                this.prevStyles = e.getSpans(start, start+count, CharacterStyle.class); 

                for (CharacterStyle c : this.prevStyles)
                {
                    if (c instanceof StyleSpan)
                    {
                        if (((StyleSpan)c).getStyle() == Typeface.BOLD) 
                            boldButton.setChecked(true);
                        else
                            boldButton.setChecked(false);
                    }
                }
            }
            else
                isDeletion = false;
        }
    }
}

0 个答案:

没有答案