0
点赞
收藏
分享

微信扫一扫

Android WebView 不能弹出alert的对话框


加载WebView弹框没有弹出来,百思不得其解,后来发现是Android WebView会阻止alert对话框弹出。如何才能让它不阻止呢,解决方法如下:

mWebview.setWebChromeClient(new WebChromeClient(){
@Override
public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
return super.onJsAlert(view, url, message, result);
}
});

问题成功解决!

源码中WebChromeClient类的onJsAlert方法默认返回false:

/**
* Tell the client to display a javascript alert dialog. If the client
* returns {@code true}, WebView will assume that the client will handle the
* dialog. If the client returns {@code false}, it will continue execution.
* @param view The WebView that initiated the callback.
* @param url The url of the page requesting the dialog.
* @param message Message to be displayed in the window.
* @param result A JsResult to confirm that the user hit enter.
* @return boolean Whether the client will handle the alert dialog.
*/
public boolean onJsAlert(WebView view, String url, String message,
JsResult result) {
return false;
}

当我们覆盖父类WebChromeClient的方法onJsAlert,设置为true,也是不行的。那么整个页面的焦点都在alert这里,这个页面触摸将会没有任何反应。只有返回super.onJsAlert(view,url,message,result)时,才能在需要alert弹框时才获取焦点,其余的由WebView自己处理。

谢谢阅读


举报

相关推荐

弹出对话框

0 条评论