mWebView =(WebView) findViewById(R.id.webview); WebSettings webSettings = mWebView.getSettings();
// 设置与Js交互的权限
webSettings.setJavaScriptEnabled(
true
);
// 设置允许JS弹窗
webSettings.setJavaScriptCanOpenWindowsAutomatically(
true
);
// 先载入JS代码
// 格式规定为:file:///android_asset/文件名.html
mWebView.loadUrl(
"file:///android_asset/javascript.html"
); button = (Button) findViewById(R.id.button); button.setOnClickListener(
new
View.OnClickListener() {
@Override
public
void
onClick
(View v) {
// 必须另开线程进行JS方法调用(否则无法调用)
mWebView.post(
new
Runnable() {
@Override
public
void
run
() {
// 注意调用的JS方法名要对应上
// 调用javascript的callJS()方法
mWebView.loadUrl(
"javascript:callJS()"
); } }); } });
// 由于设置了弹窗检验调用结果,所以需要支持js对话框
// webview只是载体,内容的渲染需要使用webviewChromClient类去实现
// 通过设置WebChromeClient对象处理JavaScript的对话框
//设置响应js 的Alert()函数
mWebView.setWebChromeClient(
new
WebChromeClient() {
@Override
public
boolean
onJsAlert
(WebView view, String url, String message,
final
JsResult result) { AlertDialog.Builder b =
new
AlertDialog.Builder(MainActivity.
this
); b.setTitle(
"Alert"
); b.setMessage(message); b.setPositiveButton(android.R.string.ok,
new
DialogInterface.OnClickListener() {
@Override
public
void
onClick
(DialogInterface dialog,
int
which) { result.confirm(); } }); b.setCancelable(
false
); b.create().show();
return
true
; } });