没有粘贴富文本格式到的EditText [英] Paste without rich text formatting into EditText

查看:609
本文介绍了没有粘贴富文本格式到的EditText的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我复制从Chrome中/粘贴文字为Android进入我的EditText查看它被搞砸了,显然是由于丰富的文本格式。

If I copy/paste text from Chrome for Android into my EditText view it gets messed up, apparently due to rich text formatting.

有没有办法告诉的EditText视图忽略丰富的文本格式?或者,我可以赶上粘贴事件之前它被设置删除吗?我会怎么做呢?

Is there a way to tell the EditText view to ignore rich text formatting? Or can I catch the paste event and remove it before it gets set? How would I do that?

更新:
于是我意识到, editText.getText()给我一个 SpannableString 包含一些格式。 就可以了,我可以通过调用 .clearSpans()摆脱这一点。但我不能做 editText.addTextChangedListener这样的事情(新TextWatcher(){...} ,因为它变得非常慢,只有UI更新,当我离开EDITTEXT视图。

UPDATE: So I realized that the editText.getText() gives me a SpannableString that contains some formatting. I can get rid of that by calling .clearSpans(); on it. BUT I cannot do anything like that in editText.addTextChangedListener(new TextWatcher() { … } because it gets terribly slow and the UI only updates when I leave the editText view.

推荐答案

问题clearSpans()是,它去掉过多,EDITTEXT此后的行为怪异。按这个答案我只删除 MetricAffectingSpan ,它工作正常呢。

The problem with clearSpans() was that it removed too much and the editText behaves weird thereafter. By following the approach in this answer I only remove the MetricAffectingSpan and it works fine then.

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

这篇关于没有粘贴富文本格式到的EditText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆