在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 (将#修改为@)