webview使用总结(一)

来源:互联网 发布:linux怎么删文件夹 编辑:程序博客网 时间:2024/06/11 14:49
1.当只用WebView的时候,最先注意的当然是在配置文件中添加访问因特网的权限; 

2.如果访问的页面中有Javascript,必须设置支持Javascript: 
 
Java代码  收藏代码
  1. webview.getSettings().setJavaScriptEnabled(true);  


3.如果希望点击链接由自己处理而不是新开Android的系统browser中响应该链接.给WebView添加一个事件监听对象(WebViewClient)并重写其中的一些方法 shouldOverrideUrlLoading对网页中超链接按钮的响应 
Java代码  收藏代码
  1. mWebView.setWebViewClient(new WebViewClient() {  
  2. /** 
  3. * Show in webview not system webview. 
  4. */  
  5. public boolean shouldOverrideUrlLoading(WebView view, String url) {  
  6.     view.loadUrl(url);  
  7.     return super.shouldOverrideUrlLoading(view, url);  
  8. }  
  9. }  

    这样就保证了每次打开的页面都是在WebView实例中显示运行的; 

4.在显示WebView时,点击手机Back时,会完全退出当前Activity,如果想退到历史浏览页面:重写back监听: 
Java代码  收藏代码
  1. public boolean onKeyDown(int keyCode, KeyEvent event) {  
  2.  WebView mWebView = (WebView) findViewById(R.id.browser);  
  3.  if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {  
  4.      mWebView.goBack();  
  5.      return true;  
  6.   }  
  7.  return super.onKeyDown(keyCode, event);  
  8. }  


5.Android SDK提供了一个schema前缀为"file:///android_asset/".WebView遇到这样的schema,就去当前包中的 assets目录中找内容.如:"file:///android_asset/demo.html" 
下面一段代码是对网页中JS的类似Alert()类的函数进行相应的重写响应: 
Java代码  收藏代码
  1. webView.setWebChromeClient(new WebChromeClient() {  
  2.             public boolean onJsAlert(WebView view, String url, String message,  
  3.                     final JsResult result) {  
  4.                 AlertDialog.Builder b = new AlertDialog.Builder(BrowserJs.this);  
  5.                 b.setTitle("Alert");  
  6.                 b.setMessage(message);  
  7.                 b.setPositiveButton(android.R.string.ok,  
  8.                         new AlertDialog.OnClickListener() {  
  9.                             public void onClick(DialogInterface dialog,  
  10.                                     int which) {  
  11.                                 result.confirm();  
  12.                             }  
  13.                         });  
  14.                 b.setCancelable(false);  
  15.                 b.create();  
  16.                 b.show();  
  17.                 return true;  
  18.             };  
  19.   
  20.             @Override  
  21.             public boolean onJsConfirm(WebView view, String url,  
  22.                     String message, final JsResult result) {  
  23.                 AlertDialog.Builder b = new AlertDialog.Builder(BrowserJs.this);  
  24.                 b.setTitle("Confirm");  
  25.                 b.setMessage(message);  
  26.                 b.setPositiveButton(android.R.string.ok,  
  27.                         new AlertDialog.OnClickListener() {  
  28.                             public void onClick(DialogInterface dialog,  
  29.                                     int which) {  
  30.                                 result.confirm();  
  31.                             }  
  32.                         });  
  33.                 b.setNegativeButton(android.R.string.cancel,  
  34.                         new DialogInterface.OnClickListener() {  
  35.                             public void onClick(DialogInterface dialog,  
  36.                                     int which) {  
  37.                                 result.cancel();  
  38.                             }  
  39.                         });  
  40.                 b.setCancelable(false);  
  41.                 b.create();  
  42.                 b.show();  
  43.                 return true;  
  44.             };  
  45.   
  46.             @Override  
  47.             public boolean onJsPrompt(WebView view, String url, String message,  
  48.                     String defaultValue, final JsPromptResult result) {  
  49.                 final LayoutInflater factory = LayoutInflater  
  50.                         .from(BrowserJs.this);  
  51.                 final View v = factory.inflate(  
  52.                         R.layout.prompt_dialog, null);  
  53.                 ((TextView) v.findViewById(R.id.prompt_message_text))  
  54.                         .setText(message);  
  55.                 ((EditText) v.findViewById(R.id.prompt_input_field))  
  56.                         .setText(defaultValue);  
  57.   
  58.                 AlertDialog.Builder b = new AlertDialog.Builder(BrowserJs.this);  
  59.                 b.setTitle("Prompt");  
  60.                 b.setView(v);  
  61.                 b.setPositiveButton(android.R.string.ok,  
  62.                         new AlertDialog.OnClickListener() {  
  63.                             public void onClick(DialogInterface dialog,  
  64.                                     int which) {  
  65.                                 String value = ((EditText) v  
  66.                                         .findViewById(R.id.prompt_input_field))  
  67.                                         .getText().toString();  
  68.                                 result.confirm(value);  
  69.                             }  
  70.                         });  
  71.                 b.setNegativeButton(android.R.string.cancel,  
  72.                         new DialogInterface.OnClickListener() {  
  73.                             public void onClick(DialogInterface dialog,  
  74.                                     int which) {  
  75.                                 result.cancel();  
  76.                             }  
  77.                         });  
  78.                 b.setOnCancelListener(new DialogInterface.OnCancelListener() {  
  79.                     public void onCancel(DialogInterface dialog) {  
  80.                         result.cancel();  
  81.                     }  
  82.                 });  
  83.                 b.show();  
  84.                 return true;  
  85.             };  
  86.   
  87.             public void onProgressChanged(WebView view, int newProgress) {  
  88.                 BrowserJs.this.getWindow().setFeatureInt(  
  89.                         Window.FEATURE_PROGRESS, newProgress * 100);  
  90.                 super.onProgressChanged(view, newProgress);  
  91.             }  
  92.   
  93.             public void onReceivedTitle(WebView view, String title) {  
  94.                 BrowserJs.this.setTitle(title);  
  95.                 super.onReceivedTitle(view, title);  
  96.             }  
  97.         });  
  98.   
  99.         go.setOnClickListener(new OnClickListener() {  
  100.             public void onClick(View view) {  
  101.                 String url = text.getText().toString();  
  102.                 webView.loadUrl(url);  
  103.             }  
  104.         });  
  105.         webView.loadUrl("file:///android_asset/index.html");  


在上述代码中,用到的prompt_dialog.xml: 
Java代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:gravity="center_horizontal" android:orientation="vertical"  
  4.     android:layout_width="fill_parent" android:layout_height="wrap_content">  
  5.     <TextView android:id="@+id/prompt_message_text"  
  6.         android:layout_width="fill_parent" android:layout_height="wrap_content" />  
  7.     <EditText android:id="@+id/prompt_input_field"  
  8.         android:layout_width="fill_parent" android:layout_height="wrap_content"  
  9.         android:selectAllOnFocus="true" android:scrollHorizontally="true"  
  10.         android:minWidth="250dp" />  
  11. </LinearLayout>  


还有assets中的Html文件: 
Java代码  收藏代码
  1. <html>  
  2. <script type="text/javascript">  
  3.   function onAlert(){  
  4.       alert("This is a alert sample from html");  
  5.   }  
  6.   function onConfirm(){  
  7.       var b=confirm("are you sure to login?");  
  8.       alert("your choice is "+b);  
  9.   }  
  10.   function onPrompt(){  
  11.       var b=prompt("please input your password","aaa");  
  12.       alert("your input is "+b);  
  13.   }  
  14. </script>  
  15. <pre>  
  16. <input type="button" value="alert" onclick="onAlert()"/>  
  17. <input type="button" value="confirm" onclick="onConfirm()"/>  
  18. <input type="button" value="prompt" onclick="onPrompt()"/>  
  19.   
  20. <a href="http://www.google.com"/>Google</a>  
  21. </pre>  
  22. </html>  
原创粉丝点击