如何在现有的 webview android 中添加自定义按钮?

Posted

技术标签:

【中文标题】如何在现有的 webview android 中添加自定义按钮?【英文标题】:How to add custom button in existing webview android? 【发布时间】:2021-12-26 15:41:17 【问题描述】:

我想在 webview 的特定位置添加自定义下载按钮。像下面的图像自定义按钮应该添加除了复制按钮

【问题讨论】:

具体你是用java还是kotlin @JohnMelodyMelissa java 太棒了!作为下面的答案:- 这个问题也需要加#java吗? 请编辑问题以将其限制为具有足够详细信息的特定问题,以确定适当的答案。 【参考方案1】:

首先你需要为ActionBarFollow this tutorial创建Icon,函数如下,

ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); 
ClipData clip = ClipData.newPlainText("label", "Text to copy");
clipboard.setPrimaryClip(clip);

对于自定义(html)里面的webView只需添加:

let copy = (msg) => 
        let textArea = document.createElement("#"); // name of the element
        textArea.value = msg;
        textArea.style.opacity = "0";
        document.body.appendChild(textArea);
        textArea.focus();
        textArea.select();


        try 
            let successful = document.execCommand('copy');
            let msg = successful ? 'successful' : 'unsuccessful';
            alert(msg);
         catch (err) 
            alert('invalid', error: ' + err.msg);
            

            document.body.removeChild(textArea);
        ;

【讨论】:

我想在 webview 中添加按钮,而不是使用 webview 的脚本 哦,我明白了,您应该特别提到这一点。等我回答另一个。那么你的意思是你打算在javasciprt 中使用脚本,但在webview 内部?介意解释一下吗? @JohnMelodyMelissa tbh,他确实特别提到了这一点 是的,我需要自定义 webview 并添加按钮。如果有任何示例或链接如何做到这一点,请给我完整的流程 @JohnMelodyMelissa,谢谢

以上是关于如何在现有的 webview android 中添加自定义按钮?的主要内容,如果未能解决你的问题,请参考以下文章