2012-11-01 48 views
0

我有一些html代码,我在Webview中显示,它有一个阅读更多的链接在它的点击,它把我带到默认浏览器。但是我想改变这种行为并在不同的活动中打开该链接,为此我想检测HTML代码中该链接的点击。我尝试了下面的代码,但无济于事。检测HTML数据中的<a>标记的点击?

Inside onCreate registerForContextMenu(w);

WebViewClient

@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 
    } 
} 
+0

http://stackoverflow.com/questions/11188348/android-html-anchor-link-works-only-once-in-webview/11205345#11205345阅读这个答案。 – MKJParekh

回答

0

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

您会在第二个参数中获得点击的网址。

查看此链接了解更多详情。

WebViewClient (shouldOverrideUrlLoading)

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

+0

谢谢你,给我看了正确的方法! – Shrikant