从web视图捕捉导航事件视图、事件、web

2023-09-08 09:45:11 作者:★小丑

我有一个web视图,有装进去一些自定义的HTML。在此HTML有一个链接到一个图像。当用户点击该链接现在,它会自动打开默认的Andr​​oid浏览器的图像。我反而想捕获click事件,并在自己的视图中打开的URL,以在UI中preserve品牌。

I have a webview that has some custom HTML loaded into it. In this HTML there is a link to an image. Right now when the user clicks the link, it automatically opens the image in the default android browser. I would instead like to capture that click event and open the URL in my own view as to preserve branding within the UI.

在iOS的,这是通过使用UIWebViewDelegate做,有没有类似的东西了Android的WebView功能?

In iOS this is done by using a UIWebViewDelegate, is there something similar for android's WebView?

推荐答案

创建 WebViewClient 的子类,并覆盖 shouldOverrideUrlLoading()。然后,通过 setWebViewClient重视这个类的一个实例,以你的的WebView ()。这适用于简单的链接和服务器发出的重定向。

Create a subclass of WebViewClient and override shouldOverrideUrlLoading(). Then, attach an instance of that class to your WebView via setWebViewClient(). This works for simple hyperlinks and server-issued redirects.