天天看點

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;
    }      

繼續閱讀