在Android的webview中定製js的alert,confirm和prompt對話方塊的方法

銳湃發表於2015-12-29
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);
	}
}


轉自:http://gundumw100.iteye.com/blog/1158719



相關文章