- 一般手機對輸入框禁止長按就可以禁止複制行為
setLongClickable(false); //禁止長按 setTextIsSelectable(false); // 禁止被使用者選擇
-
但個别手機會出現粘貼選項框,要對輸入框禁止粘貼,TextView.class中方法onTextContextMenuItem(int id)
`
/** * Called when a context menu option for the text view is selected. Currently
* this will be one of {@link android.R.id#selectAll}, {@link android.R.id#cut},
* {@link android.R.id#copy}, {@link android.R.id#paste} or {@link android.R.id#shareText}.
* @return true if the context menu item action was performed.
*/
public boolean onTextContextMenuItem(int id){
…………………………
switch (id) {
case ID_SELECT_ALL:
selectAllText();
return true;
case ID_UNDO:
if (mEditor != null) {
mEditor.undo();
}
return true; // Returns true even if nothing was undone.
case ID_REDO:
if (mEditor != null) {
mEditor.redo();
}
return true; // Returns true even if nothing was undone.
case ID_PASTE:
paste(min, max, true /* withFormatting */);
return true;
case ID_PASTE_AS_PLAIN_TEXT:
paste(min, max, false /* withFormatting */);
return true;
case ID_CUT:
setPrimaryClip(ClipData.newPlainText(null, getTransformedText(min, max)));
deleteText_internal(min, max);
return true;
case ID_COPY:
setPrimaryClip(ClipData.newPlainText(null, getTransformedText(min, max)));
stopTextActionMode();
return true;
case ID_REPLACE:
if (mEditor != null) {
mEditor.replace();
}
return true;
case ID_SHARE:
shareSelectedText();
return true;
}
return false;
}
EditText繼承TextView,隻要重寫onTextContextMenuItem(int id)方法,對粘貼方法不做響應,即可實作不粘貼功能
@Override
public boolean onTextContextMenuItem(int id) {
if (id == android.R.id.paste) {
return false;
}
return super.onTextContextMenuItem(id);
} `