Enable longClick in WebView

后端 未结 4 1727
误落风尘
误落风尘 2020-11-29 01:02

In the browser, you can longClick on URLs. In my WebView, you cannot. How can I make it so you can?

4条回答
  •  [愿得一人]
    2020-11-29 01:57

    I just wanted to copy URL data on long click.
    By taking reference from the accepted answer I wrote this.

        registerForContextMenu(webView);
    
    
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
    
        WebView webView = (WebView) v;
        HitTestResult result = webView.getHitTestResult();
    
        if (result != null) {
            if (result.getType() == HitTestResult.SRC_ANCHOR_TYPE) {
                String linkToCopy = result.getExtra();
                copyToClipboard(linkToCopy, AppConstants.URL_TO_COPY);
            }
        }
    }
    

提交回复
热议问题