如何使用HitTestResult在Android WebView中使用Longclick获取链接图像(而不是图像URL)的链接URL [英] How to get link-URL in Android WebView with HitTestResult for a linked image (and not the image-URL) with Longclick

查看:29
本文介绍了如何使用HitTestResult在Android WebView中使用Longclick获取链接图像(而不是图像URL)的链接URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试捕捉 webview longclicks 以显示上下文菜单.(见下面的代码)长按图像时,我总是将图像 URL 作为额外的(对于带有 IMAGE_TYPE 的未链接图像和带有 SRC_IMAGE_ANCHOR_TYPE 的链接图像).但是,如何获取带有超链接的图像的链接 URL(而不是图像 URL)?

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?

最好的,塞巴斯蒂安

        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;
            }
        });

推荐答案

我检查了 WebView 的源代码,似乎图像 uri 是您可以为 SRC_IMAGE_ANCHOR_TYPE 获得的唯一额外数据.但是不要生气,我有一个快速而肮脏的解决方法给你:

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;
        }
    });

我在低端 Android 2.1 设备和高端 Android 4.0 设备上尝试了代码,两者都非常有用.

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.

问候

陈子腾

这篇关于如何使用HitTestResult在Android WebView中使用Longclick获取链接图像(而不是图像URL)的链接URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆