复制 EditText 文本导致的崩溃:PARAGRAPH span must end at paragraph boundary

问题现象
复制 WebView 的文本(其实重点是多个段落) -> 粘贴 到输入框;
尝试复制输入框里面的文本(全选、复制),应用抛出异常:

2018-11-01 11:18:29.519 21987-21987/xxx.xxx.xx E/MtaSDK.CaughtExp: java.lang.RuntimeException: PARAGRAPH span must end at paragraph boundary (62 follows  )
        at android.text.SpannableStringInternal.setSpan(SpannableStringInternal.java:171)
        at android.text.SpannableStringInternal.copySpans(SpannableStringInternal.java:68)
        at android.text.SpannableStringInternal.(SpannableStringInternal.java:43)
        at android.text.SpannedString.(SpannedString.java:30)
        at android.text.method.ReplacementTransformationMethod$SpannedReplacementCharSequence.subSequence(ReplacementTransformationMethod.java:180)
        at android.widget.TextView.getTransformedText(TextView.java:9529)
        at android.widget.TextView.onTextContextMenuItem(TextView.java:9484)
        at android.widget.Editor$TextActionModeCallback.onActionItemClicked(Editor.java:4031)
        at com.android.internal.policy.DecorView$ActionModeCallback2Wrapper.onActionItemClicked(DecorView.java:2393)
        at com.android.internal.view.FloatingActionMode$3.onMenuItemSelected(FloatingActionMode.java:88)
        at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:761)
        at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
        at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:904)
        at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:894)
        at com.android.internal.view.FloatingActionMode$4.onMenuItemClick(FloatingActionMode.java:114)
        at com.android.internal.widget.FloatingToolbar$FloatingToolbarPopup$3.onClick(FloatingToolbar.java:398)
        at android.view.View.performClick(View.java:5642)
        at android.view.View$PerformClick.run(View.java:22485)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6211)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:793)

其实我觉得跟应用没关系,奈何有的手机有问题,有的没有问题。而且分析了一下,只要文本跨段落,就会有问题,所以还是尝试着解决。

问题原因:其实网上有介绍,就是文本包含段落,但是不是以换行结尾。
我 debug 的时候发现,如果输入框设置了单行输入,复制段落到 TextEdit 里面的时候,文本会保留 "\n",只不过显示的时候系统做了处理。参见“SingleLineTransformationMethod”。

解决方法:既然是文本包含 "\n" 的原因,那就想办法把 "\n" 替换掉吧。
我的办法是给 EditText 增加一个 InputFilter ,替换掉 "\n"。

在调用过 super 的构造方法后添加。因为 TextView 会根据 XML 里面的 maxLength 添加一个 InputFilter.LengthFilter(maxlength),不能覆盖掉。

    private void addReplaceEndOfLineFilter() {
        InputFilter replaceEndOfLineFilter = new InputFilter() {
            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                if (source == null) {
                    return null;
                }
                source = source.toString().replaceAll("\n", " ");
                return source;
            }
        };

        InputFilter[] filters = getFilters();
        List filtersList = new ArrayList<>();
        if (filters != null) {
            filtersList.addAll(Arrays.asList(filters));
        }
        filtersList.add(replaceEndOfLineFilter);
        setFilters(filtersList.toArray(filters));
    }

你可能感兴趣的:(复制 EditText 文本导致的崩溃:PARAGRAPH span must end at paragraph boundary)