webchromeclient = new SelfWebChromeClient( progressBar); webView.setWebChromeClient(webchromeclient);SelfWebChromeClient继承自WebChromeClient :
class SelfWebChromeClient extends WebChromeClient {}
@Override public void onReceivedTitle(WebView view, String title) { titleText.setText(title); }此方法返回了页面的title,你可以在此方法体内展示此title信息。
需要继承并实现WebChromeClient的onProgressChanged方法
@Override public void onProgressChanged(WebView view, int newProgress) { // 当WebView进度改变时更新窗口进 if (progressbar.getVisibility() != View.VISIBLE&&newProgress != 100) { progressbar.setVisibility(View.VISIBLE); } progressbar.setProgress(newProgress); super.onProgressChanged(view, newProgress); }
此方法返回了页面加载的进度newProgress。
alert("大家好");这时候需要如何来处理呢?WebChromeClient提供了回调方法onJsAlert,继承并实现此方法,在此方法体中做你想做的事,例如:
@Override public boolean onJsAlert(WebView view, String url, String message, final JsResult result) { // 构建一个Builder来显示网页中的alert对话框 Builder builder = new Builder(TestWebActivity.this); builder.setTitle("alert对话框"); builder.setMessage(message); builder.setPositiveButton(android.R.string.ok, new AlertDialog.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { result.confirm(); } }); builder.setCancelable(false); builder.create(); builder.show(); return true; }
还有诸如onJsConfirm之类的方法同理。
private View customView; class SelfWebChromeClient extends WebChromeClient { private Bitmap defaltvideo; private ProgressBar progressbar; private CustomViewCallback myCallback = null; public SelfWebChromeClient( ProgressBar progressbar) { super(); this.progressbar = progressbar; } @Override public void onShowCustomView(View view, CustomViewCallback callback) { if (myCallback != null) { myCallback.onCustomViewHidden(); myCallback = null; return; } setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); ViewGroup parent = (ViewGroup) webView.getParent(); parent.removeView(webView); parent.addView(view); webviewFrame.addView(webView); customView = view; myCallback = callback; } public void onHideCustomView() { if (customView != null) { if (myCallback != null) { myCallback.onCustomViewHidden(); myCallback = null; } try { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); ViewParent pat = customView.getParent(); ViewGroup parent = (ViewGroup) pat; parent.removeView(customView); webviewFrame.removeAllViews(); parent.addView(webView); customView = null; } catch (Exception e) { e.printStackTrace(); } } } // 视频加载添加默认图标 @Override public Bitmap getDefaultVideoPoster() { System.out.println("getDefaultVideoPoster-->>"); if (defaltvideo == null) { defaltvideo = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); } return defaltvideo; } // 视频加载时进程loading @Override public View getVideoLoadingProgressView() { System.out.println("getVideoLoadingProgressView-->>"); ProgressBar progressbar = new ProgressBar(TestWebActivity.this, null, android.R.attr.progressBarStyleHorizontal); progressbar.setLayoutParams(new AbsoluteLayout.LayoutParams( LayoutParams.FILL_PARENT, 3, 0, 0)); return progressbar; } }
@Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { if (customView != null) { webchromeclient.onHideCustomView(); return true; } else { if (webView.canGoBack()) { webView.goBack(); return true; } else { finish(); } } } return false; }
@Override protected void onResume() { // LogActs.i("onResume-->>"); try { webView.getClass().getMethod("onResume") .invoke(webView, (Object[]) null); } catch (Exception e) { e.printStackTrace(); } super.onResume(); } @Override protected void onStop() { try { webView.getClass().getMethod("onPause") .invoke(webView, (Object[]) null); } catch (Exception e) { e.printStackTrace(); } // webView.loadUrl("about:blank"); super.onStop(); }
android WebView(五)WebChromeClient
原文地址:http://blog.csdn.net/w2865673691/article/details/44982985