1、对webview封装,在遇到加载页面时可以调用。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:orientation="vertical" >
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<WebView
android:id="@+id/webView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<LinearLayout
android:id="@+id/ll_loading_dialog"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:background="#88000000"
android:orientation="vertical"
android:visibility="visible" >
<ProgressBar
android:layout_width="60dip"
android:layout_height="60dip" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/data_loading"
android:textColor="@android:color/white"
android:textSize="@dimen/font_body_12" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
2、java代码
public class Web {
public Web(Activity activity) {
super();
this.activity = activity;
initWebView();
}
private Activity activity;
private LinearLayout mLoadingLayout;
private WebView mWebView;
/**
* 加载详情
*/
private void initWebView() {
mWebView = (WebView) activity.findViewById(R.id.webView);
mLoadingLayout = (LinearLayout) activity
.findViewById(R.id.ll_loading_dialog);
mWebView.setBackgroundColor(0);
WebSettings webSettings = mWebView.getSettings();
mWebView.requestFocus();
webSettings.setJavaScriptEnabled(true);
mWebView.setFocusable(true);
webSettings.setBuiltInZoomControls(true);
mWebView.setScrollBarStyle(0);
mWebView.setWebChromeClient(new WebChromeClient() {
@Override
public void onProgressChanged(WebView view, int newProgress) {
Logger.getLogger().i("-----web------"+newProgress);
if (newProgress>=100) {
mWebView.loadUrl(method);
Logger.getLogger().i("-----web load finish ------> "+newProgress);
}
super.onProgressChanged(view, newProgress);
}
@Override
public boolean onJsAlert(WebView view, String url, String message,
final JsResult result) {
final AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
builder.setMessage(message).setPositiveButton("确定", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
result.confirm();
}
});
builder.setOnKeyListener(new OnKeyListener() {
public boolean onKey(DialogInterface dialog, int keyCode,KeyEvent event) {
return true;
}
});
// 禁止响应按back键的事件
builder.setCancelable(false);
AlertDialog dialog = builder.create();
dialog.show();
return true;
}
});
mWebView.setWebViewClient(new WebViewClient() {
// 重写父类方法,让新打开的网页在当前的WebView中显示
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
loadUrl(view, url);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
mLoadingLayout.setVisibility(View.GONE);
super.onPageFinished(view, url);
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
mLoadingLayout.setVisibility(View.VISIBLE);
super.onPageStarted(view, url, favicon);
}
});
}
public void loadUrl(String url) {
loadUrl(mWebView, url);
}
private void loadUrl(final WebView webView, final String url) {
webView.loadUrl(url);
}
/**
* 加载方法,网页加载完毕时加载方法,防止加载不到方法
* @param method
*/
public void loadMethod(String method){
Logger.getLogger().i(method);
this.method = method;
}
private String method=null;
}
3、调用
Web mWeb = new Web(this);
mWeb.loadUrl("url");
mWeb.loadMethod("javascript:show()");
1.首先继承android.webkit.WebChromeClient实现MyWebChromeClient。
2.在MyWebChromeClient.java中覆盖onJsAlert,onJsConfirm,onJsPrompt三个方法。
3.在初始化Webview时设置调用webview.setWebChromeClient(new MyWebChromeClient());
4.在Webview载入的html中使用window.alert,window.confirm,window.prompt方法,系统弹出的将是自定义实现的对应对话框。
MyWebChromeClient.java的代码如下:
- import android.app.AlertDialog;
- import android.content.DialogInterface;
- import android.content.DialogInterface.OnCancelListener;
- import android.content.DialogInterface.OnClickListener;
- import android.content.DialogInterface.OnKeyListener;
- import android.graphics.Bitmap;
- import android.os.Message;
- import android.util.Log;
- import android.view.KeyEvent;
- import android.webkit.JsPromptResult;
- import android.webkit.JsResult;
- import android.webkit.WebChromeClient;
- import android.webkit.WebView;
- import android.widget.EditText;
- /**
- * http://618119.com/archives/2010/12/20/199.html
- */
- //****************************************************************************
- public class MyWebChromeClient extends WebChromeClient {
- @Override
- public void onCloseWindow(WebView window) {
- super.onCloseWindow(window);
- }
- @Override
- public boolean onCreateWindow(WebView view, boolean dialog,
- boolean userGesture, Message resultMsg) {
- return super.onCreateWindow(view, dialog, userGesture, resultMsg);
- }
- /**
- * 覆盖默认的window.alert展示界面,避免title里显示为“:来自file:”
- */
- public boolean onJsAlert(WebView view, String url, String message,
- JsResult result) {
- final AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
- builder.setTitle("对话框")
- .setMessage(message)
- .setPositiveButton("确定", null);
- // 不需要绑定按键事件
- // 屏蔽keycode等于84之类的按键
- builder.setOnKeyListener(new OnKeyListener() {
- public boolean onKey(DialogInterface dialog, int keyCode,KeyEvent event) {
- Log.v("onJsAlert", "keyCode==" + keyCode + "event="+ event);
- return true;
- }
- });
- // 禁止响应按back键的事件
- builder.setCancelable(false);
- AlertDialog dialog = builder.create();
- dialog.show();
- result.confirm();// 因为没有绑定事件,需要强行confirm,否则页面会变黑显示不了内容。
- return true;
- // return super.onJsAlert(view, url, message, result);
- }
- public boolean onJsBeforeUnload(WebView view, String url,
- String message, JsResult result) {
- return super.onJsBeforeUnload(view, url, message, result);
- }
- /**
- * 覆盖默认的window.confirm展示界面,避免title里显示为“:来自file:”
- */
- public boolean onJsConfirm(WebView view, String url, String message,
- final JsResult result) {
- final AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
- builder.setTitle("对话框")
- .setMessage(message)
- .setPositiveButton("确定",new OnClickListener() {
- public void onClick(DialogInterface dialog,int which) {
- result.confirm();
- }
- })
- .setNeutralButton("取消", new OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- result.cancel();
- }
- });
- builder.setOnCancelListener(new OnCancelListener() {
- @Override
- public void onCancel(DialogInterface dialog) {
- result.cancel();
- }
- });
- // 屏蔽keycode等于84之类的按键,避免按键后导致对话框消息而页面无法再弹出对话框的问题
- builder.setOnKeyListener(new OnKeyListener() {
- @Override
- public boolean onKey(DialogInterface dialog, int keyCode,KeyEvent event) {
- Log.v("onJsConfirm", "keyCode==" + keyCode + "event="+ event);
- return true;
- }
- });
- // 禁止响应按back键的事件
- // builder.setCancelable(false);
- AlertDialog dialog = builder.create();
- dialog.show();
- return true;
- // return super.onJsConfirm(view, url, message, result);
- }
- /**
- * 覆盖默认的window.prompt展示界面,避免title里显示为“:来自file:”
- * window.prompt('请输入您的域名地址', '618119.com');
- */
- public boolean onJsPrompt(WebView view, String url, String message,
- String defaultValue, final JsPromptResult result) {
- final AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
- builder.setTitle("对话框").setMessage(message);
- final EditText et = new EditText(view.getContext());
- et.setSingleLine();
- et.setText(defaultValue);
- builder.setView(et)
- .setPositiveButton("确定", new OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- result.confirm(et.getText().toString());
- }
- })
- .setNeutralButton("取消", new OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- result.cancel();
- }
- });
- // 屏蔽keycode等于84之类的按键,避免按键后导致对话框消息而页面无法再弹出对话框的问题
- builder.setOnKeyListener(new OnKeyListener() {
- public boolean onKey(DialogInterface dialog, int keyCode,KeyEvent event) {
- Log.v("onJsPrompt", "keyCode==" + keyCode + "event="+ event);
- return true;
- }
- });
- // 禁止响应按back键的事件
- // builder.setCancelable(false);
- AlertDialog dialog = builder.create();
- dialog.show();
- return true;
- // return super.onJsPrompt(view, url, message, defaultValue,
- // result);
- }
- @Override
- public void onProgressChanged(WebView view, int newProgress) {
- super.onProgressChanged(view, newProgress);
- }
- @Override
- public void onReceivedIcon(WebView view, Bitmap icon) {
- super.onReceivedIcon(view, icon);
- }
- @Override
- public void onReceivedTitle(WebView view, String title) {
- super.onReceivedTitle(view, title);
- }
- @Override
- public void onRequestFocus(WebView view) {
- super.onRequestFocus(view);
- }
- }