其他分享
首页 > 其他分享> > android – 将没有富文本格式的粘贴粘贴到EditText中

android – 将没有富文本格式的粘贴粘贴到EditText中

作者:互联网

如果我将Chrome for Android中的文本复制/粘贴到我的EditText视图中,它会变得混乱,显然是由于富文本格式化.

The font size is totally messed up and not that big http://erikmi.tk/WaeG/image.png

有没有办法告诉EditText视图忽略富文本格式?或者我可以捕获粘贴事件并在设置之前将其删除吗?我该怎么办?

更新:
所以我意识到editText.getText()给了我一个包含一些格式的SpannableString.我可以通过调用.clearSpans();在上面.但是我不能在editText.addTextChangedListener(new TextWatcher(){…}中做任何类似的事情,因为它变得非常慢而且UI只在我离开editText视图时更新.

解决方法:

clearSpans()的问题在于它删除了太多而且editText后来表现得很奇怪.通过遵循this answer中的方法,我只删除MetricAffectingSpan,然后它可以正常工作.

public void afterTextChanged(Editable string)
{
    CharacterStyle[] toBeRemovedSpans = string.getSpans(0, string.length(),
                                                MetricAffectingSpan.class);
    for (int index = 0; index < toBeRemovedSpans; index++)
        string.removeSpan(toBeRemovedSpans[index]);
    }
}

标签:richtext,android,rtf,android-edittext
来源: https://codeday.me/bug/20191006/1861162.html