Back to project page jsWebView.
The source code is released under:
Apache License
If you think the Android project jsWebView listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package wjj.js; /*w w w . j a v a2s . com*/ 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 { /** * ?????window.alert????????????title?????????file:////?? */ @Override public boolean onJsAlert(WebView view, String url, String message, JsResult result) { AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext()); builder.setTitle("alert????").setMessage(message) .setPositiveButton("??", null);// ??????????alert????????????????listener????null // ???keycode??84????? builder.setOnKeyListener(new OnKeyListener() { @Override 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,???????????????????????????????alert????????????????alert?????? return true; } @Override public boolean onJsBeforeUnload(WebView view, String url, String message, JsResult result) { return super.onJsBeforeUnload(view, url, message, result); } /** * ?????window.confirm????????????title?????????file:////?? */ @Override public boolean onJsConfirm(WebView view, String url, String message, final JsResult result) { final AlertDialog.Builder builder = new AlertDialog.Builder( view.getContext()); builder.setTitle("confirm????").setMessage(message) .setPositiveButton("??", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { result.confirm(); } }).setNeutralButton("????", new OnClickListener() { @Override 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; } /** * ?????window.prompt????????????title?????????file:////?? * window.prompt('?????????????', '618119.com'); */ @Override 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("prompt????").setMessage(message); final EditText et = new EditText(view.getContext()); et.setSingleLine(); et.setText(defaultValue); builder.setView(et); builder.setPositiveButton("??", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { result.confirm(et.getText().toString()); } }).setNeutralButton("????", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { result.cancel(); } }); // ???keycode??84???????????????????????????????????????????? builder.setOnKeyListener(new OnKeyListener() { @Override 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; } }