1.首先在h5中加入
<head> <style> .unselectable { -moz-user-select: none; -webkit-user-select: none; } </style> </head> <body> <div class="unselectable" unselectable="on"> The user is not able to select this text in Firefox, Google Chrome and Safari. In Internet Explorer and Opera, it is selectable only if the selection starts outside. </div> </body>
2.系统默认会有长按效果。
3.如果需要自定义效果
这个功能首先可以从两方面完成:
(1) 在js中完成:
处理android.selection.longTouch
(2) 安卓层处理:
首先使用OnTouchListener实现长按实现监听,然后实现WebView的Context menu,最后调用webview中的emulateShiftHeld(),为了适配安卓不同版本,最好使用反射方式调用.
开源项目参考:
这里推荐一个开源项目进行参考,:
本文介绍了如何在WebView中实现长按选择复制的功能,分别从JavaScript和Android层面进行讲解。在JS中处理android.selection.longTouch事件,而在Android层通过OnTouchListener监听长按,结合WebView的Context Menu与emulateShiftHeld()方法(使用反射调用),以兼容不同版本的Android系统。
7968

被折叠的 条评论
为什么被折叠?



