我正在写一个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;
}
}
}