需要使用WebChromeClient
:
webview.setWebChromeClient(new WebChromeClient());
甚至还能覆盖alert
的实现:
webview.setWebChromeClient(new WebChromeClient() {
@Override
public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
//Required functionality here
return super.onJsAlert(view, url, message, result);
}
});
更新
实际开发中还会需要查看js
报错,
这时候就需要重载onConsoleMessage
:
@Override
public void onConsoleMessage(String message, int lineNumber, String sourceID) {
// TODO Auto-generated method stub
Log.v("ChromeClient", "invoked: onConsoleMessage() - " + sourceID + ":"
+ lineNumber + " - " + message);
super.onConsoleMessage(message, lineNumber, sourceID);
}