当用户在网页浏览中按某些内容时,我需要获取网址。 我使用这段代码:
webView = (WebView) rootView.findViewById(R.id.webView);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.loadUrl("my url");
webView.setWebViewClient(new MyWebClient());
MyWebClien
private class MyWebClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.e("URL", url);
return true;
}
}
但是,如果我点击某些内容,则永远不会调用该事件。 谢谢
答案 0 :(得分:5)
shouldOverrideUrlLoading(WebView视图,String url)。
用法示例:
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
try {
// do whatever you want to do on a web link click
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
});
webView.loadUrl(“在webview上打开第一个网页的网址”);
答案 1 :(得分:2)
基于此:how to get the current page url from the web view in android
WebView webview = new WebView(context);
webview.setWebViewClient(new WebViewClient()
{
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
System.out.println("your current url when webpage loading.." + url);
}
@Override
public void onPageFinished(WebView view, String url) {
System.out.println("your current url when webpage loading.. finish" + url);
super.onPageFinished(view, url);
}
@Override
public void onLoadResource(WebView view, String url) {
// TODO Auto-generated method stub
super.onLoadResource(view, url);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
System.out.println("when you click on any interlink on webview that time you got url :-" + url);
return super.shouldOverrideUrlLoading(view, url);
}
});