开发者

Android: Prevent Clipboard Copy of Text

Our application requires that the text 开发者_运维技巧displayed in TextViews or WebViews is not copyable. Can we disable the copy function for controls in an activity or application?


Try this on TextViews, This mechanism is same in WebView as well.

textView.cancelLongPress();
webView.cancelLongPress();

This should work, as user will not be able to trigger onLongClickListner. Hope this will help to achieve in shortest possible code.

Happy Coding!


The copy/paste/select..etc dialog shows up when you do long press on the text area. What if you override onLongClickListener for your view and do nothing inside? Or use ClipboardManager and listen for primary clip changes, if the changes occur then check if there is text or anything else, if true then use setPrimaryClip() with ClipData instance that contains let say empty string. This not might be the best approach, but these pointers maybe will give you the idea.

http://developer.android.com/reference/android/content/ClipboardManager.html

http://developer.android.com/reference/android/content/ClipData.html


The below worked for me:

textView.setLongClickable(false);


For some reason none of the other answers worked for me so here is my take on it. I specifically used it on a Webview :

webview.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {

            public void onCreateContextMenu(ContextMenu menu, View v,
                    ContextMenuInfo menuInfo) {
                menu.clear();

            }
        });

Hope it helps someone.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜