机器人的WebView - 截点击 [英] Android WebView - Intercept clicks
问题描述
我写了一个简单的HelloWorld应用与web视图具有一个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>
如何在我的活动我抓住这个点击,从航行,然后告知http://CNN.com点击的活动停止的WebView?
How, in my Activity do I capture the click on this, stop the WebView from navigating, and then informing the Activity that "http://CNN.com" was clicked?
推荐答案
然后,你必须设置一个<一个href="http://developer.android.com/reference/android/webkit/WebViewClient.html"><$c$c>WebViewClient$c$c>你的的WebView
并重写<一href="http://developer.android.com/reference/android/webkit/WebViewClient.html#shouldOverrideUrlLoading%28android.webkit.WebView,%20java.lang.String%29"><$c$c>shouldOverrideUrlLoading$c$c>和<一href="http://developer.android.com/reference/android/webkit/WebViewClient.html#onLoadResource%28android.webkit.WebView,%20java.lang.String%29"><$c$c>onLoadResource$c$c>的方法。让我给你举一个简单的例子:
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
}
}
}
这篇关于机器人的WebView - 截点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!