webView.setWebViewClient(new MyWebViewClient());
public class MyWebViewClient extends WebViewClient {
private ProgressDialog loadingBar;
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
loadingBar=ProgressDialog.show(mActivity, null, "正在加载…");
super.onPageStarted(view, url, favicon);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url); // webview里面的链接还在同一个页面中加载
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
if(loadingBar.isShowing()){
loadingBar.dismiss();
}
super.onPageFinished(view, url);
}
@Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
Toast.makeText(mActivity, "加载出错!", Toast.LENGTH_LONG).show();
final AlertDialog alertDialog = new AlertDialog.Builder(mActivity).create();
alertDialog.setTitle("ERROR");
alertDialog.setMessage(description);
alertDialog.setButton("OK", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
alertDialog.dismiss();
}
});
alertDialog.show();
}
}