微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在MIUI的小米设备中双击编辑文本时,“复制”选项弹出

如何解决在MIUI的小米设备中双击编辑文本时,“复制”选项弹出

具有Miui的小米设备未禁用复制弹出窗口。我用下面的代码。但这不起作用。该解决方案适用于除小米以外的所有设备。

enter image description here

这是我的自定义EditText类

public class MyCustomEditText extends EditText {

public MyCustomEditText(Context context) {
    super(context);
}

public MyCustomEditText(Context context,AttributeSet attrs) {
    super(context,attrs);
}

public MyCustomEditText(Context context,AttributeSet attrs,int defStyleAttr) {
    super(context,attrs,defStyleAttr);
}

boolean canPaste() {
    return false;
}

@Override
public int getSelectionStart() {
    for (StackTraceElement element : Thread.currentThread().getStackTrace()) {
        if (element.getmethodName().equals("canPaste")) {
            return -1;
        }
    }
    return super.getSelectionStart();
}

@Override
public boolean isSuggestionsEnabled() {
    return false;
}

@Override
protected void onCreateContextMenu(ContextMenu menu) {
    if (menu != null) {
        menu.clear();
    }
}

@Override
public boolean onTextContextMenuItem(int id) {
    switch (id) {
        case android.R.id.paste:
        case android.R.id.pasteAsPlainText:
            return false;

    }
    return super.onTextContextMenuItem(id);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        // setInsertiondisabled when user touches the view
        this.setInsertiondisabled();
    }
    return super.onTouchEvent(event);
}

private void setInsertiondisabled() {
    try {
        Field editorField = TextView.class.getDeclaredField("mEditor");
        editorField.setAccessible(true);
        Object editorObject = editorField.get(this);

        Class editorClass = Class.forName("android.widget.Editor");
        Field mInsertionControllerEnabledField = 
        editorClass.getDeclaredField("mInsertionControllerEnabled");
        mInsertionControllerEnabledField.setAccessible(true);
        mInsertionControllerEnabledField.set(editorObject,false);
    } catch (Exception ignored) {
        // ignore exception here
    }
}

}

添加了用于编辑文本的代码

        editText.setLongClickable(false);
        editText.setTextIsSelectable(false);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            editText.setImportantForAutofill(View.IMPORTANT_FOR_AUTOFILL_NO);
        }

        ActionMode.Callback callback = new ActionMode.Callback() {
            @Override
            public boolean onCreateActionMode(ActionMode mode,Menu menu) {
                if (menu != null) {
                    menu.removeItem(android.R.id.paste);
                    menu.removeItem(android.R.id.copy);
                }
                LLog.d("TESTINGG","onCreateActionMode");
                return false;
            }

            @Override
            public boolean onPrepareActionMode(ActionMode mode,Menu menu) {
                if (menu != null) {
                    menu.clear();
                }
                LLog.d("TESTINGG","onPrepareActionMode");
                return false;
            }

            @Override
            public boolean onActionItemClicked(ActionMode mode,MenuItem item) {
                LLog.d("TESTINGG","onActionItemClicked");
                return false;
            }

            @Override
            public void onDestroyActionMode(ActionMode mode) {

            }
        };

        editText.setCustomSelectionActionModeCallback(callback);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            editText.setCustomInsertionActionModeCallback(callback);
        }

注意:-在密码模式下使用密码时,该代码在所有设备(包括小米)上均能正常工作。 见下图:

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。