問題描述
我對 Android 開發領域還很陌生,最近遇到了一個棘手的問題.
I am quite new to Android developing area and recently I hv encountered a tough problem.
我正在嘗試制作一個不應允許用戶從中復制內容或將內容粘貼到其中的 Edittext.我用谷歌搜索了很多,發現似乎有兩種流行的方法:
I was trying to make a Edittext which should NOT ALLOW user to copy content from or paste content to it. I hv googled a lot and find there seems to be 2 popular ways of doing so:
第一種方式,在布局文件中設置:
1st way, to set it in the layout file:
android:longClickable="false"
第二種方式,以編程方式設置它:
2nd way, to programmatically set it:
myEdittext.setCustomSelectionActionModeCallback(new ActionMode.Callback() {
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
public void onDestroyActionMode(ActionMode mode) {
}
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
return false;
}
public boolean onActionItemClicked(ActionMode mode,
MenuItem item) {
return false;
}
});
但我發現無論我選擇哪種方式,edittext 區域都只能被禁用長按,這會阻止用戶通過長按訪問全選,復制和粘貼"菜單.但是這兩種解決方案都沒有阻止用戶通過簡單地點擊光標來訪問粘貼"功能.
But I just found that whichever way I chose, the edittext area could only be disabled from long clickable, which then prevents user from accessing the "select all, copy and paste" menu through long clicking. But both the 2 solution DID NOT prevent the user from accessing the "paste" function through just a simple tap on the cursor.
所以我的問題是:我怎么能完全阻止用戶在某個 Edittext 中使用復制和粘貼功能.有人幫忙嗎?非常感謝
So my question is: how could I TOTALLY block user from copy and paste function in a certain Edittext. Is anyone help? Thx a lot
推薦答案
有一種可能,通過禁用游標處理程序.您將無法獲得粘貼按鈕,但您也無法通過觸摸移動光標.
There is one possibility, by disabling the cursor handler. You won't get the paste button, but you will also not be able to move the cursor with touch.
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getActionMasked() == MotionEvent.ACTION_UP && mDisableCursorHandle) {
// Hack to prevent keyboard and insertion handle from showing.
cancelLongPress();
}
return super.onTouchEvent(event);
}
這篇關于Android:如何在 Edittext 中完全禁用復制和粘貼功能的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!