Paste without rich text formatting into EditText

后端 未结 4 632
一个人的身影
一个人的身影 2020-12-16 12:09

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

Is there a way to tell the EditText vie

4条回答
  •  没有蜡笔的小新
    2020-12-16 12:47

    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.

    For me the only problem was the size of the text. If you have other problems you'd have to adjust what you want to remove.

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

提交回复
热议问题