1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
| 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;
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); }
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); builder.setOnKeyListener(new OnKeyListener() { public boolean onKey(DialogInterface dialog, int keyCode,KeyEvent event) { Log.v("onJsAlert", "keyCode==" + keyCode + "event="+ event); return true; } }); builder.setCancelable(false); AlertDialog dialog = builder.create(); dialog.show(); result.confirm(); return true; } public boolean onJsBeforeUnload(WebView view, String url, String message, JsResult result) { return super.onJsBeforeUnload(view, url, message, result); }
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(); } }); builder.setOnKeyListener(new OnKeyListener() { @Override public boolean onKey(DialogInterface dialog, int keyCode,KeyEvent event) { Log.v("onJsConfirm", "keyCode==" + keyCode + "event="+ event); return true; } }); AlertDialog dialog = builder.create(); dialog.show(); return true; }
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(); } }); builder.setOnKeyListener(new OnKeyListener() { public boolean onKey(DialogInterface dialog, int keyCode,KeyEvent event) { Log.v("onJsPrompt", "keyCode==" + keyCode + "event="+ event); return true; } }); AlertDialog dialog = builder.create(); dialog.show(); return true; } @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); } }
|