Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 获得;链接文本“;在WebView中显示的链接的_Android_Google Chrome_Android Webview_Android Menu - Fatal编程技术网

Android 获得;链接文本“;在WebView中显示的链接的

Android 获得;链接文本“;在WebView中显示的链接的,android,google-chrome,android-webview,android-menu,Android,Google Chrome,Android Webview,Android Menu,我有一个Android WebView,它显示了一些链接:现在我想在长按这些链接时检索Link1Text和Link2Text。我在代码中实现了contextMenu,我可以使用HitTestResult getExtra()方法成功获取链接URL(,),但我如何获取这些链接文本呢?仅供参考,我需要这些链接文本来实现contextMenu中的“复制链接文本”选项。不幸的是,一个清晰的,目前还没有正式的方法来做到这一点。尽管有两个API(selectText和copySelection)正在等待AP

我有一个Android WebView,它显示了一些链接:


现在我想在长按这些链接时检索Link1TextLink2Text。我在代码中实现了contextMenu,我可以使用HitTestResult getExtra()方法成功获取链接URL(,),但我如何获取这些链接文本呢?

仅供参考,我需要这些链接文本来实现contextMenu中的“复制链接文本”选项。

不幸的是,一个清晰的,目前还没有正式的方法来做到这一点。尽管有两个API(selectText和copySelection)正在等待API委员会的批准,这可能有助于实现这一点,但它们目前不可用。

要获取achor链接的文本: I.通过
evaluateJavascript
将touchstart侦听器挂接到WebViewClient的onPageFinished()回调中的每个网页。比如:

        //Javascripts to evaluate in onPageFinished

        const w=window;
        w.addEventListener('touchstart',wrappedOnDownFunc);
        function wrappedOnDownFunc(e){
            if(e.touches.length==1){
                w._touchtarget = e.touches[0].target;
            }
            console.log('hey touched something ' +w._touchtarget);
        }
注意,我们已保存触摸目标

二,。然后为webview实现OnLongClicklisenter。长按链接对象时再次使用
evaluateJavascript

            @Override
            public boolean onLongClick(View v) {
                WebView.HitTestResult result = ((WebView)v).getHitTestResult();
                if (null == result) return false;
                int type = result.getType();
                switch (type) {
                    case WebView.HitTestResult.SRC_ANCHOR_TYPE:
                        if(result.getExtra()!=null){
                            ((WebView)v).evaluateJavascript("window._touchtarget?window._touchtarget.innerText:''", new ValueCallback<String>() {
                                @Override
                                public void onReceiveValue(String value) {
                                    System.out.println("hey received link text : "+value);
                                }
                            });
                        }
                    return true;
                }
                return false;
            }
最后,在
onLongClick
侦听器中,我们不只是获取
innerText
,而是通过编程方式设置选择,触发操作菜单栏,并恢复
触摸目标的已删除href属性

    case WebViewmy.HitTestResult.SRC_ANCHOR_TYPE:
        if(result.getExtra()!=null){
            WebViewmy mWebView = ((WebViewmy)v);
            mWebView.evaluateJavascript("selectTouchtarget()", new ValueCallback<String>() {
                @Override
                public void onReceiveValue(String value) {
                    /* bring in action mode by a fake click on the programmatically  selected text. */
                    MotionEvent te = MotionEvent.obtain(0,0,KeyEvent.ACTION_DOWN,mWebView.lastX,mWebView.lastY,0);
                    mWebView.dispatchTouchEvent(te);
                    te.setAction(KeyEvent.ACTION_UP);
                    mWebView.dispatchTouchEvent(te);
                    te.recycle();
                    //if it's not delayed for a while or the href attribute is not removed, then the above code would click into
                    //  the anchor element instead of select it's text.
                    /* restore href attribute */
                    mWebView.postDelayed(() -> mWebView.evaluateJavascript("restoreTouchtarget()", null), 100);
                }
            });
        }
    return true;

case WebViewmy.HitTestResult.SRC\u锚类型:
if(result.getExtra()!=null){
WebViewmy mWebView=((WebViewmy)v);
evaluateJavascript(“selectTouchtarget()”,new ValueCallback()){
@凌驾
公共无效onReceiveValue(字符串值){
/*通过在编程选择的文本上假单击进入操作模式*/
MotionEvent te=MotionEvent.Acquire(0,0,KeyEvent.ACTION_向下,mWebView.lastX,mWebView.lastY,0);
mWebView.dispatchTouchEvent(te);
te.设置动作(KeyEvent.动作启动);
mWebView.dispatchTouchEvent(te);
te.recycle();
//如果它没有延迟一段时间,或者href属性没有被删除,那么上面的代码将点击进入
//锚定元素,而不是选择其文本。
/*restore href属性*/
mWebView.postDelayed(()->mWebView.evaluateJavascript(“restoreTouchtarget()”,null),100);
}
});
}
返回true;

在我的例子中,我将WebView扩展为WebViewmy,以便在onTouchEvent方法中记录最后触摸的位置lastX和lastY

你收到解决方案和链接文本了吗?
    case WebViewmy.HitTestResult.SRC_ANCHOR_TYPE:
        if(result.getExtra()!=null){
            WebViewmy mWebView = ((WebViewmy)v);
            mWebView.evaluateJavascript("selectTouchtarget()", new ValueCallback<String>() {
                @Override
                public void onReceiveValue(String value) {
                    /* bring in action mode by a fake click on the programmatically  selected text. */
                    MotionEvent te = MotionEvent.obtain(0,0,KeyEvent.ACTION_DOWN,mWebView.lastX,mWebView.lastY,0);
                    mWebView.dispatchTouchEvent(te);
                    te.setAction(KeyEvent.ACTION_UP);
                    mWebView.dispatchTouchEvent(te);
                    te.recycle();
                    //if it's not delayed for a while or the href attribute is not removed, then the above code would click into
                    //  the anchor element instead of select it's text.
                    /* restore href attribute */
                    mWebView.postDelayed(() -> mWebView.evaluateJavascript("restoreTouchtarget()", null), 100);
                }
            });
        }
    return true;