android webview网页上的点击按钮(事件gotointroducion)跳转url链接地址无效没有反应

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_34900897/article/details/83585444

本来这个是个简单事情,可就是便便搞了几个小时,比如,webview加载网页,里面有个按钮点击跳转到另一个网页,当点击的时候按钮是有反应的,但网页log打印gotointroducion,有点蒙蔽,不知道什么意思?百度也没有,搜索了很久。用了很多种方法都没有解决,最后还是艰难的解决了。

一些js和java交互这些的基本设置就不贴上代码了;大概意思是什么重定向问题。

可以看看这些

https://www.jianshu.com/p/a12b5aab41c5

https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Redirections

https://blog.csdn.net/lastdream/article/details/40512779

这都是一些重定向解释。

里面有一个网页按钮点击跳转到重定向另一个网页,按钮调用方法,但是无法重定向另一个页面,需要重写

需要重写shouldOverrideUrlLoading方法

webView.setWebViewClient(new WebViewClient() {
			@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
				 view.loadUrl(url);
			return true;
		}

});

里面的这个会出现过时

另一方法

 @RequiresApi(Build.VERSION_CODES.LOLLIPOP)//api大于21
 override fun shouldOverrideUrlLoading(view: WebView?, request:WebResourceRequest?):Boolean 
      {
             view?.loadUrl(request?.url.toString())
             return super.shouldOverrideUrlLoading(view, request)
       }

这种方法也可以

最后决定还是两种方法都一起用

/**
     * Give the host application a chance to take over the control when a new
     * url is about to be loaded in the current WebView. If WebViewClient is not
     * provided, by default WebView will ask Activity Manager to choose the
     * proper handler for the url. If WebViewClient is provided, return true
     * means the host application handles the url, while return false means the
     * current WebView handles the url.
     *
     * <p>Notes:
     * <ul>
     * <li>This method is not called for requests using the POST &quot;method&quot;.</li>
     * <li>This method is also called for subframes with non-http schemes, thus it is
     * strongly disadvised to unconditionally call {@link WebView#loadUrl(String)}
     * with the request's url from inside the method and then return true,
     * as this will make WebView to attempt loading a non-http url, and thus fail.</li>
     * </ul>
     * </p>
     *
     * @param view The WebView that is initiating the callback.
     * @param request Object containing the details of the request.
     * @return True if the host application wants to leave the current WebView
     *         and handle the url itself, otherwise return false.
     */
    public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
        return shouldOverrideUrlLoading(view, request.getUrl().toString());
    }

两个方法的参数大概意思相同;

view 回调webview视图

request 加载url地址

如果程序要离开当前视图webview,返回true,并处理本身,否则返回false,而post请求,不调用此方法。应该是点击网页按钮的时候,返回false,不处理url链接,所以没有跳转页面。

猜你喜欢

转载自blog.csdn.net/qq_34900897/article/details/83585444