如何在 Webview 中实现自己的 SelectAll、Cut、Copy、Paste

Posted

技术标签:

【中文标题】如何在 Webview 中实现自己的 SelectAll、Cut、Copy、Paste【英文标题】:How to implement your own SelectAll,Cut,Copy,Paste in Webview 【发布时间】:2013-05-30 05:33:51 【问题描述】:

我正在使用 WebView 实现富编辑文本,用于编辑器操作的按钮显示在上下文操作栏 (CAB) 中,在长按 webview 时,它的默认 CAB 显示按钮,我知道 Webview 不提供任何规定自定义它的 CAB。

所以我认为最好实现选择、剪切、复制、粘贴操作。 我已经尝试过this 但对我不起作用。

我的应用程序面向 android 3.x+ 设备。

请建议我如何做到这一点。

【问题讨论】:

【参考方案1】:

我使用BTAndroidWebViewSelection javascript 库来执行此操作。对于剪切、复制和粘贴操作,我注入了自己的 JavaScript。

全选 - (function () document.execCommand('selectall', true, null);)() 剪切 - (function () document.execCommand('cut', true, null);)() 复制 - (function () document.execCommand('copy', true, null);)() 粘贴 - document.execCommand('inserthtml', false,'" + text + "'); 文本是从剪贴板复制的数据。

谢谢。

【讨论】:

请注意,由于跨域安全策略,这不适用于您文档中位于不同域的 iframe。

以上是关于如何在 Webview 中实现自己的 SelectAll、Cut、Copy、Paste的主要内容,如果未能解决你的问题,请参考以下文章

如何在 android webview 中实现 phonegap/cordova?

在 webview 片段中实现后退按钮

在 ViewPager Webview 中实现点击功能

在android蜂窝webview/webkit中实现水平/垂直手指滑动滚动?

我可以使用 flutter_webview_plugin 在 appBar 中实现菜单吗?

如何在UWP应用中实现Google登录?