EditText 屏蔽选择、复制、粘贴等一切剪切板的操作

时间:2022-03-30 16:51:51

EditText 如何屏蔽选择、复制、粘贴等一切剪切板的操作呢?

找到的资料,总结起来都是这的:

editText.setLongClickable(false);
editText.setCustomSelectionActionModeCallback(new ActionModeCallbackInterceptor());
 private class ActionModeCallbackInterceptor implements ActionMode.Callback {
private final String TAG = NoMenuEditText.class.getSimpleName();


public boolean onCreateActionMode(ActionMode mode, Menu menu) {
return false;
}


public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}


public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
return false;
}


public void onDestroyActionMode(ActionMode mode) {
}
}

然而,这只能满足部分需求,只能屏蔽选择、复制等行为;而对粘贴的行为,无法起到动作。经过查看EditText源码方法,举一反三,再添加如下代码,可屏蔽粘贴行为:

editText.setCustomInsertionActionModeCallback( new ActionModeCallbackInterceptor());

修正:
setCustomInsertionActionModeCallback此方法在小于Build.VERSION_CODES.M的版本中,不兼容,所以需要以下判断:

        if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.M) {
// call that method
this.setCustomInsertionActionModeCallback( new ActionModeCallbackInterceptor());
}