且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何获得链接URL Android中的WebView与HitTestResult为链接的图像(而不是图像URL)与Longclick

更新时间:2023-02-26 12:52:06

我查了的WebView源$ C ​​$ c和它似乎像URI是唯一的额外数据,你可以得到SRC_IMAGE_ANCHOR_TYPE。但是,不要被疯的,我有一个快速和肮脏的解决方法为您提供:

  webview.setOnLongClickListener(新OnLongClickListener(){
        @覆盖
        公共布尔onLongClick(视图v){
            最后的WebView的WebView =(的WebView)V;
            最终HitTestResult结果= webview.getHitTestResult();
            如果(result.getType()== HitTestResult.SRC_IMAGE_ANCHOR_TYPE){
                webview.setWebViewClient(新WebViewClient(){
                    @覆盖
                    公共布尔shouldOverrideUrlLoading(web视图查看,字符串URL){
                        // 2。在这里,我们得到的网址(记得要删除的WebView客户端,并返回true,这样的超级链接不会被真正触发)
                        mUrl =网址; // mUrl是活动的成员变
                        view.setWebViewClient(空);
                        返回true;
                    }
                });
                // 1.画面一定要专注,所以我们模拟一个DPAD进入事件触发的超级链接
                KeyEvent的EVENT1 =新的KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.KEY code_DPAD_CENTER);
                webview.dispatchKeyEvent(EVENT1);
                KeyEvent的EVENT2 =新的KeyEvent(KeyEvent.ACTION_UP,KeyEvent.KEY code_DPAD_CENTER);
                webview.dispatchKeyEvent(EVENT2);
                // 3。现在你可以做一些与锚URL(并清除mUrl以便将来使用)
                字符串URL = mUrl;
                如果(网址!= NULL){
                    Toast.makeText(webview.getContext(),网址,Toast.LENGTH_SHORT).show();
                }

                mUrl = NULL;
            }
            返回false;
        }
    });
 

我试图在低端的Andr​​oid 2.1设备和高端的Andr​​oid 4.0设备,两者的工作就像一个魅力的code。

问候

紫藤陈

I try to catch webview longclicks to show a context menu. (see code below) When longclicking an image, I always get the image-URL as extra (for a not linked image with IMAGE_TYPE and for a linked image with SRC_IMAGE_ANCHOR_TYPE). But how can I get the Link-URL (and not the image-URL) for an image with a hyperlink?

Best, Sebastian

        mywebview.setOnLongClickListener(new OnLongClickListener() {
            public boolean onLongClick(View v) {

                final WebView webview = (WebView) v;
                final WebView.HitTestResult result = webview.getHitTestResult();

                if (result.getType() == SRC_ANCHOR_TYPE) {
                    return true;
                }

                if (result.getType() == SRC_IMAGE_ANCHOR_TYPE) {
                    return true;
                }

                if (result.getType() == IMAGE_TYPE) {
                    return true;
                }

                return false;
            }
        });

I checked the source code of the WebView and it seems that the image uri is the only extra data you can get for SRC_IMAGE_ANCHOR_TYPE. But don't be mad here I have a quick and dirty workaround for you:

    webview.setOnLongClickListener(new OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            final WebView webview = (WebView) v;
            final HitTestResult result = webview.getHitTestResult();
            if(result.getType()==HitTestResult.SRC_IMAGE_ANCHOR_TYPE) {
                webview.setWebViewClient(new WebViewClient(){
                    @Override
                    public boolean shouldOverrideUrlLoading(WebView view, String url) {
                        // 2. and here we get the url (remember to remove the WebView client and return true so that the hyperlink will not be really triggered)
                        mUrl = url; // mUrl is a member variant of the activity
                        view.setWebViewClient(null);
                        return true;
                    }
                });
                // 1. the picture must be focused, so we simulate a DPAD enter event to trigger the hyperlink
                KeyEvent event1 = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_CENTER);
                webview.dispatchKeyEvent(event1);
                KeyEvent event2 = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DPAD_CENTER);
                webview.dispatchKeyEvent(event2);
                // 3. now you can do something with the anchor url (and then clear the mUrl for future usage)
                String url = mUrl;
                if (url!=null) {
                    Toast.makeText(webview.getContext(), url, Toast.LENGTH_SHORT).show();
                }

                mUrl = null;
            }
            return false;
        }
    });

I tried the code on a low-end Android 2.1 device and a high-end Android 4.0 device, both work like a charm.

Regards

Ziteng Chen