WebView new 一個 WebViewClient 的話,可以處理
- onReceivedError-網頁讀取失敗
- onPageStarted-開始讀取網頁
- onPageFinished-網頁讀取結束
- shouldOverrideUrlLoading-是否用自定瀏覽器開啟連結
mWebView.setWebViewClient(new WebViewClient() {
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
// super.onReceivedError(view, errorCode, description, failingUrl);
view.loadData("<html></html>", "text/html", null);
findViewById(R.id.LayoutError).setVisibility(View.VISIBLE);
findViewById(R.id.ButtonRefresh).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
findViewById(R.id.LayoutError).setVisibility(View.GONE);
initLayout();
}
});
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
mProgressDialog = new ProgressDialog(EventActivity.this);
mProgressDialog.setMessage(getResources().getString(R.string.reading));
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
mProgressDialog.show();
super.onPageStarted(view, url, favicon);
}
@Override
public void onPageFinished(WebView view, String url) {
mProgressDialog.dismiss();
super.onPageFinished(view, url);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
沒有留言:
張貼留言