android WebView中js的alert()失效
WebView的设置代码
wv = (WebView) findViewById(R.id.webView1); wv.getSettings().setJavaScriptEnabled(true);//支持js wv.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);//允许js弹出窗口 wv.loadUrl("http://www.google.com"); wv.setWebViewClient(new WebViewClient());
还需要做如下设置,通过这样的转换为android内部的控件的
webView.setWebChromeClient(new WebChromeClient() { @Override public boolean onJsAlert(WebView view, String url, String message, final JsResult result) { AlertDialog.Builder b2 = new AlertDialog.Builder(this) .setTitle(R.string.title).setMessage(message) .setPositiveButton("ok", new AlertDialog.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { result.confirm(); // MyWebView.this.finish(); } });
b2.setCancelable(false); b2.create(); b2.show(); return true; } }