24
我已经使用WebView编写了一个简单的helloworld应用程序,该应用程序在我的资产文件夹的simple.html页面上有一个链接到CNN。Android WebView - 拦截点击
<a href="http://cnn.com">cnn.com</a>
我怎样才能捕捉到这个点击我的活动,从航行停止的WebView,然后通知“http://CNN.com”被点击的活动?
我已经使用WebView编写了一个简单的helloworld应用程序,该应用程序在我的资产文件夹的simple.html页面上有一个链接到CNN。Android WebView - 拦截点击
<a href="http://cnn.com">cnn.com</a>
我怎样才能捕捉到这个点击我的活动,从航行停止的WebView,然后通知“http://CNN.com”被点击的活动?
然后,您必须将WebViewClient
设置为WebView
并覆盖shouldOverrideUrlLoading
和onLoadResource
方法。让我举个简单的例子:
WebView yourWebView; // initialize it as always...
// this is the funny part:
yourWebView.setWebViewClient(yourWebClient);
// somewhere on your code...
WebViewClient yourWebClient = new WebViewClient(){
// you tell the webclient you want to catch when a url is about to load
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
return true;
}
// here you execute an action when the URL you want is about to load
@Override
public void onLoadResource(WebView view, String url){
if(url.equals("http://cnn.com")){
// do whatever you want
}
}
}
对不起,我知道这是陈旧的,但它正是我正在寻找的除了....我的导航没有完成。当用户单击WebView中的链接时,它会转到shouldOverrideLoading并执行该操作,但不会完成导航。我希望它完成只是想嗅一下他们先选择什么。 – GPGVM 2012-05-04 18:01:52
在这种情况下,你不应该在shouldOverrideUrlLoading – Cristian 2012-05-04 18:14:31
中返回true,所以返回false,除非你真的想要中止。 – GPGVM 2012-05-04 18:17:22