'Adding alert() support to a WebView'에 해당되는 글 1건

  1. 2010.05.04 Adding alert() support to a WebView
04.Anddoid2010. 5. 4. 14:18
반응형

As promised earlier, here is an example of how to add support for alert() function to a WebView in your Android application:

  1.  
  2. final WebView browser = (WebView)findViewById(R.id.browser);  
  3. /* JavaScript must be enabled if you want it to work, obviously */ 
  4. browser.getSettings().setJavaScriptEnabled(true);  
  5.  
  6. final Context myApp = this;  
  7.  
  8. /* WebChromeClient must be set BEFORE calling loadUrl! */ 
  9. browser.setWebChromeClient(new WebChromeClient() {  
  10.     @Override 
  11.     public boolean onJsAlert(WebView view, String url, String message, final android.webkit.JsResult result)  
  12.     {  
  13.         new AlertDialog.Builder(myApp)  
  14.             .setTitle("javaScript dialog")  
  15.             .setMessage(message)  
  16.             .setPositiveButton(android.R.string.ok,  
  17.                     new AlertDialog.OnClickListener()  
  18.                     {  
  19.                         public void onClick(DialogInterface dialog, int which)  
  20.                         {  
  21.                             result.confirm();  
  22.                         }  
  23.                     })  
  24.             .setCancelable(false)  
  25.             .create()  
  26.             .show();  
  27.  
  28.         return true;  
  29.     };  
  30. });  
  31.  
  32. /* load a web page which uses the alert() function */ 
  33. browser.loadUrl("http://lexandera.com/files/jsexamples/alert.html");  

Code for adding support for confirm() and prompt() is almost identical and can be found in Mosembro’s source code.


출처 : http://lexandera.com/2009/01/adding-alert-support-to-a-webview/

Posted by 1010