简体   繁体   English

<a>从HTML数据中</a>检测到点击<a>标记?</a>

[英]Detect a click on <a> tag from HTML data?

I am having some html code which i am displaying in Webview, it has a read more link in it on click of which it takes me to default browser. 我正在Webview中显示一些html代码,单击该链接后会有更多阅读链接,将其带到默认浏览器。 But i want to change this behavior and open that link in different activity, for which i want to detect the click on that link within the HTML code. 但是我想更改此行为并在其他活动中打开该链接,为此我想检测HTML代码中该链接的单击。 I tried the following code but to no avail. 我尝试了以下代码,但无济于事。

Inside onCreate registerForContextMenu(w); 内部onCreate registerForContextMenu(w);

and after that 在那之后

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);

    WebView.HitTestResult result = w.getHitTestResult();

    System.out.println("asdasfdf");

    if (result.getType() == HitTestResult.ANCHOR_TYPE
            || result.getType() == HitTestResult.SRC_ANCHOR_TYPE) {

        System.out.println("#%@#%$%");
        // set the header title to the link url
    }
}

shouldOverrideUrlLoading method of WebViewClient will do the trick. WebViewClient shouldOverrideUrlLoading方法可以解决问题。

yourWebView.setWebViewClient(new WebViewClient() {  
  @Override  
  public boolean shouldOverrideUrlLoading(WebView view, String url) {  
    // Load the url in your own webView. or pass the url to another activity. and stop the current loading. check the following pages
  }
}  

You'll get the clicked url in second parameter. 您将在第二个参数中获得单击的URL。

Check this link for more detail. 检查此链接以获取更多详细信息。

WebViewClient (shouldOverrideUrlLoading) WebViewClient(shouldOverrideUrlLoading)

http://www.catchingtales.com/android-webview-shouldoverrideurlloading-and-redirect/416/ http://www.catchingtales.com/android-webview-shouldoverrideurlloading-and-redirect/416/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM