首页 > 代码库 > android webview

android webview

1.WebvView是android内置的一款webkit高性能内部浏览器

2. (1)权限:<uses-permission android:name="android.permission.INTERNET"/>

  (2)如果Html有JavaScript则需要设置: webView.getSettings().setJavaScriptEnabled(true);

  (3)如果页面中有连接,点击连接希望在Webview中继续打开,必需设置webView.setWebViewClient(new WebViewClient());否则会用调用系统的浏览器打开。

new WebViewClient(){

    @Override

    public boolean shouldOverrideUrlLoading(WebView view, String url) {

}

WebViewClient类的shouldOverrideUrlLoading方法,当打开一个连接时会调用此方法,url为要打开的网页地址.可以在特定的网页打开之前,做一些动作,也可以不打开网页

3.打开页面的方式

(1)打开互联网页面

webView.loadUrl(“http://www.google.com“);  //get形式

webView.postUrl(url,EncodingUtils.getBytes(postData,"UTF-8"));  //Post形式.url网址,postData参数:值

(2)打开本地文件

myWebView.loadUrl(“file:///android_asset/XX.html“)

文件存放在assets目录里,以file:///android_asset/开头,xx.html是文件名

(3)直接载入html字符串

myWebView.loadData(htmlString, "text/html", "utf-8");

 

4.后退与前进

按返回键时,如果不做任何处理,则会退出整个Browser,如果要返回到上一页使用webView.goBack();前进使用webView.goForward();如果达到历史顶端执行这两个动作则不会执行任何动作,所以在执行之前先进行判断:webView.canGoBack()和webView.canGoForward()。

@Override

    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if(keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()){

            webView.goBack();

            return true;

        }

        return super.onKeyDown(keyCode, event);

    }

 

 

5.WebView与javascript交互

 (1)javascript访问java代码

  java代码: webView.addJavascriptInterface(new Object(){

           public void onBack(){      

            }

       },"test");

  javascript: <a onClick="window.test.onBack ()">。

 

 通过addJavascriptInterface(Object,String)方法

test是暴露给JS的接口名,对应Object对象实例,javascript通过window.test.onBack ()调用Object的onBack()方法。

当调用Object时,会另起一个线程

 

(2)java访问javascript

mWebView.loadUrl("javascript:test(‘aa‘)"); 其中test是javascript的方法

可在test函数里将数据alert,然后webview中重写WebChromeClient的onJsAlert函数,具体代码如下

[java] view plaincopy

wv.setWebChromeClient(new MyWebChromeClient()); 

 

final  class MyWebChromeClient extends WebChromeClient{ 

 

    @Override 

 

      public booleanonJsAlert(WebView view, String url, String message, final JsResult result) {  

 

//message就是test函数里alert的字符串,这样你就可以在android客户端里对这个数据进行处理 

  

                result.confirm();         

 

              }  

 

 return true;   

        } 

 

6自定义一个带进度条的WebView

public class ProgressWebview extends WebView{

         private ProgressBar progressBar;

 

         public ProgressWebview(Context context, AttributeSet attrs) {

                   super(context, attrs);

                   progressBar = new ProgressBar(context, null,android.R.attr.progressBarStyleHorizontal);

                   progressBar.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,4,0,0));

                   addView(progressBar);

                   setWebChromeClient(new WebChrmoeClient());

         }

 

         /*

          * 显示进度条

          */

         public class WebChrmoeClient extends WebChromeClient{

                   @Override

                   public void onProgressChanged(WebView view, int newProgress) {

                            if(newProgress==100){

                                     progressBar.setVisibility(View.GONE);

                            }else {

                                     if(progressBar.getVisibility() == GONE){

                                               progressBar.setVisibility(View.VISIBLE);

                                     }

                                     progressBar.setProgress(newProgress);

                            }

                            super.onProgressChanged(view, newProgress);

                   }

                  

                  

                   @Override

                   public boolean onJsAlert(WebView view, String url, String message,

                                     JsResult result) {

                            // TODO Auto-generated method stub

                            return super.onJsAlert(view, url, message, result);

                   }

         }

        

         @Override

         protected void onScrollChanged(int l, int t, int oldl, int oldt) {

                   // TODO Auto-generated method stub

                   LayoutParams params = (LayoutParams) progressBar.getLayoutParams();

                   params.x = l;

                   params.y = t;

                   progressBar.setLayoutParams(params);

                   super.onScrollChanged(l, t, oldl, oldt);

         }

}

android webview