- 一般手機(jī)對(duì)輸入框禁止長(zhǎng)按就可以禁止復(fù)制行為
setLongClickable(false); //禁止長(zhǎng)按 setTextIsSelectable(false); // 禁止被用戶選擇
- 但個(gè)別手機(jī)會(huì)出現(xiàn)粘貼選項(xiàng)框,要對(duì)輸入框禁止粘貼,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)方法封拧,對(duì)粘貼方法不做響應(yīng)奕枢,即可實(shí)現(xiàn)不粘貼功能
@Override
public boolean onTextContextMenuItem(int id) {
if (id == android.R.id.paste) {
return false;
}
return super.onTextContextMenuItem(id);
} `