//WebView启用Javascript脚本执行
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.getSettings().setAllowFileAccess(true);// 设置允许访问文件数据
webView.setVerticalScrollbarOverlay(true);
webView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
webView.loadUrl(url);
return false;
}
});
// 如果不设置这个,JS代码中的按钮会显示,但是按下去却不弹出对话框
// Sets the chrome handler. This is an implementation of WebChromeClient
// for use in handling JavaScript dialogs, favicons, titles, and the
// progress. This will replace the current handler.
if (url==null) {
webView.loadUrl("http://www.baidu.com");
} else {
webView.loadUrl(url);
}
webView.addJavascriptInterface(new DaMiJavaScriptInteface(this), "myjs");
webView.setWebChromeClient(new WebChromeClient());
new Handler(){
@Override
public void handleMessage(Message msg) {
L.showlog("load js!!");
webView.loadUrl("javascript:window.myjs.clickOnAndroid()");
}
}.sendEmptyMessageDelayed(1, 3000);
public class DaMiJavaScriptInteface {
//好像每个方法都有@一下....
@JavascriptInterface
public void clickOnAndroid(){
T.showMessage(mactivtiy, "哈哈2");
}
}