Android WebView导航事件? [英] Android WebView navigating event?

查看:84
本文介绍了Android WebView导航事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找与WPF WebBrowser的导航"事件或IOS UiWebView的"ShouldStartLoad"等效的Android WebView事件.

I'm looking for the event of a Android WebView that is equivalent to the "Navigating" event of a WPF WebBrowser, or the "ShouldStartLoad" of a IOS UiWebView.

我找到了"shouldOverrideUrlLoading",但我不知道如何使用它,因为构造函数需要一个URL ...

I found "shouldOverrideUrlLoading", but I don't know how to use it because the constructor needs an URL...

我想在导航之前拦截网址,并取消某些URL的导航...

I would like to intercept the Url before navigating, and cancel the navigation for some URLs...

我在Visual Studio中使用Xamarin.

I'm using Xamarin in Visual Studio.

相当于WPF:

webBrowser.Navigating += WebBrowserNavigating;

private void WebBrowserNavigating(object sender, NavigatingEventArgs e)
    {
        if (//condition)
        e.Cancel = true;
        //Do something else
    }

推荐答案

是的,您应该使用 shouldOverrideUrlLoading 方法.该方法会自动调用,因此您不必担心参数. 为了使用此方法,您必须首先创建一个WebViewClient,如下所示:

Yes, you should use the shouldOverrideUrlLoading method. This method is called automatically, so you don't have to worry about the parameters. In order to use this method, you have to create first a WebViewClient like this:

private class HelloWebViewClient extends WebViewClient {

     @Override
   public boolean shouldOverrideUrlLoading(WebView view, String url) {
         ...
      }
}

然后,将其设置为WebViewClient:

And then, you set it as your WebViewClient:

webview.setWebViewClient(new HelloWebViewClient());

调用 shouldOverrideUrlLoading 方法时,将通过参数接收WebView和将要加载的URL.

When the shouldOverrideUrlLoading method is called you'll receive by parameter the WebView and the URL that is going to be loaded.

这篇关于Android WebView导航事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆