最近在使用 WebView 的 onJsAlert 方法後,發現html頁面點擊沒反應了,代碼如下:
mWebView.setWebChromeClient(new WebChromeClient(){@Overridepublic boolean onJsAlert(WebView view, String url, String message,JsResult result) {if(message!=null){//TODO//彈出對話方塊Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();}return true;}@Overridepublic boolean onJsConfirm(WebView view, String url,String message, JsResult result) {// TODO Auto-generated method stubreturn super.onJsConfirm(view, url, message, result);}@Overridepublic boolean onJsPrompt(WebView view, String url, String message,String defaultValue, JsPromptResult result) {// TODO Auto-generated method stubreturn super.onJsPrompt(view, url, message, defaultValue, result);}});
百思不得其解,最後在stackoverflow上找到了答案
http://stackoverflow.com/questions/15892644/android-webview-after-onjsalert-not-responding-taps
mWebView.setWebChromeClient(new WebChromeClient(){@Overridepublic boolean onJsAlert(WebView view, String url, String message,JsResult result) {if(message!=null){//TODO//彈出對話方塊Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();}result.cancel();//一定要cancel,否則會出現各種奇怪問題return true;}@Overridepublic boolean onJsConfirm(WebView view, String url,String message, JsResult result) {// TODO Auto-generated method stubreturn super.onJsConfirm(view, url, message, result);}@Overridepublic boolean onJsPrompt(WebView view, String url, String message,String defaultValue, JsPromptResult result) {// TODO Auto-generated method stubreturn super.onJsPrompt(view, url, message, defaultValue, result);}});
return true 和 result.cancel(); 一個都不能少,否則會出現各種奇怪的問題。