Android WebView - 拦截点击 [英] Android WebView - Intercept clicks
本文介绍了Android WebView - 拦截点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我编写了一个带有 WebView 的简单 helloworld 应用程序,该应用程序在我的资产文件夹中的 simple.html 页面上有一个指向 CNN 的链接.
I have written a simple helloworld app with a WebView which has a link to CNN on a simple.html page in my asset folder.
<a href="http://cnn.com">cnn.com</a>
如何在我的 Activity 上捕获对此的点击,停止 WebView 导航,然后通知 Activity "http://CNN.com"被点击?
How can I capture the click on this on my Activity, stop the WebView from navigating, and then inform the Activity that "http://CNN.com" was clicked?
推荐答案
那你就得设置一个 WebViewClient
到您的 WebView
并覆盖 shouldOverrideUrlLoading
和 onLoadResource
方法.举个简单的例子:
Then you have to set a WebViewClient
to your WebView
and override shouldOverrideUrlLoading
and onLoadResource
methods. Let me give you a simple example:
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
}
}
}
这篇关于Android WebView - 拦截点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文