我的英语非常不好,我在文本中有错误))))抱歉!
如何制作Webview返回上一页,并且不显示:空白? 这是我的代码:
@Override
public void onBackPressed() {
if (mWebView.canGoBack()) {
mWebView.goBack();
return;
}
else {
finish();
}
super.onBackPressed();
}
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
mbErrorOccured = true;
showErrorLayout();
super.onReceivedError(view, errorCode, description, failingUrl);
loadErrorPage();
}
}
private void showErrorLayout() {
mlLayoutRequestError.setVisibility(View.VISIBLE);
}
private void loadErrorPage() {
if(mWebView!=null){
String htmlData ="<html><body><div align= center >Check your internet!</div></body>" ;
mWebView.loadUrl("about:blank");
mWebView.loadDataWithBaseURL(null,htmlData, "text", "utf-8",null);
mWebView.invalidate();
}
}
例如,加载了Google页面,然后发生了一个错误,并显示了有关:已加载空白,并且当重新加载该页面时,WebView重新加载了关于:空白而不是Google页面。如何使Google页面在重新加载时加载?
答案 0 :(得分:0)
如果about:blank页面已经加载,然后您通过调用webView.reload()
重新加载webview,它将仅重新加载当前页面,即about:blank。
如果要加载上一页,只需调用webView.goBack()
即可,也可以像这样直接加载网址-> webView.loadUrl("<your.web.url>")
如果您想更好地控制Webview,请使用WebViewClient和WebChromeClient。
请参见下面的代码
首先创建WebViewClient并将其分配给webview。
var currentUrl : String = "https://www.google.com";
webview.webViewClient = MyWebViewClient()
在WebViewClient的shouldOverrideUrlLoading
方法中,您可以拥有自己的逻辑来决定要加载的网页。
class MyWebViewClient : WebViewClient(){
override fun onReceivedError(
view: WebView?,
request: WebResourceRequest?,
error: WebResourceError?
) {
super.onReceivedError(view, request, error)
// here display your custom error message with retry feature
displayErrorDialog()
}
override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
// update current url
if (url != null) {
currentUrl = url
}
return super.shouldOverrideUrlLoading(view, url)
}
}