WebView之加載網頁時增加進度提示


上一節講了一些webview的基本使用以及在記載網頁時如何屏蔽掉第三方瀏覽器,使我們自己開發的程序成為一個微型瀏覽器。那么這一節將一下在webView加載網頁的過程中如何加上進度提示。效果圖如下:

主要代碼:

當網頁加載時我們希望看到進度條,當網頁加載完成時取消進度條。實時的顯示進度是在WebViewChromeClient類只能夠設置的。這個類提供了一個onProgressChanged(WebView view, int newProgress) (newProgress的最大值是100最小值為0)方法用於更新進度條

代碼如下:

//顯示進度
        webView.setWebChromeClient(new WebChromeClient(){
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                Log.e("newProgress", newProgress+"");
                progressBar.setProgress(newProgress);
                if(newProgress >= 100){
                    progressBar.setVisibility(View.GONE);
                }
//                super.onProgressChanged(view, newProgress);
            }
            
        });

 

下面貼出完整的代碼:

一、布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >
     <ProgressBar 
        android:id="@+id/index_progressBar"
        android:layout_width="fill_parent"
        android:layout_height="5dp"
        style="?android:attr/progressBarStyleHorizontal"
         android:progress="0"
         android:visibility="visible"
        android:secondaryProgress="0"
        />
    <WebView 
        android:id="@+id/index_webView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        />

</LinearLayout>

其中progressbar用於顯示進度條,webview用於顯示加載的網頁

二、主類,上面我都加了詳細的注釋,如果那里不明白可以看看注釋,這里就不再贅述了。直接上代碼。

package cn.yw.sol;

import cn.yw.sol.R;
import cn.yw.sol.view.SOLWebView;
import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;

/**
 * webkit simple demo
 * @author tony
 *
 */
public class MainActivity extends Activity{
    private WebView webView;
    private ProgressBar progressBar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.index);
        webView = (WebView)findViewById(R.id.index_webView);//實例化webview對象
        progressBar = (ProgressBar)findViewById(R.id.index_progressBar);
        //設置webview屬性能夠執行javascript腳本
        webView.getSettings().setJavaScriptEnabled(true);
        //設置webView可以縮放,只可以雙擊縮放
        webView.getSettings().setSupportZoom(true);
        //設置是否可縮放
        webView.getSettings().setBuiltInZoomControls(true); 
        //無限縮放
        webView.getSettings().setUseWideViewPort(true);
        //加載需要顯示的網頁
        webView.loadUrl("http://www.baidu.com/");
        /**
        1.AndroidManifest.xml中必須使用許可"android.permission.INTERNET",否則會出Web page not available錯誤。
        2.如果訪問的頁面中有Javascript,則webview必須設置支持Javascript。
                webview.getSettings().setJavaScriptEnabled(true);  
        3.如果頁面中鏈接,如果希望點擊鏈接繼續在當前browser中響應,而不是新開Android的系統browser中響應該鏈接,必須覆蓋 webview的WebViewClient對象。
         */
        webView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                progressBar.setVisibility(View.VISIBLE);
                view.loadUrl(url);
                return true;
            }
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
            }
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
            }
            @Override
            public void onLoadResource(WebView view, String url) {
                super.onLoadResource(view, url);
            }
            @Override
            public void onReceivedError(WebView view, int errorCode,
                    String description, String failingUrl) {
                super.onReceivedError(view, errorCode, description, failingUrl);
            }
        });
        //顯示進度
        webView.setWebChromeClient(new WebChromeClient(){
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                Log.e("newProgress", newProgress+"");
                progressBar.setProgress(newProgress);
                if(newProgress >= 100){
                    progressBar.setVisibility(View.GONE);
                }
//                super.onProgressChanged(view, newProgress);
            }
            
        });
    }
    
    //如果不做任何處理,瀏覽網頁,點擊系統“Back”鍵,整個Browser會調用finish()而結束自身
    //,如果希望瀏覽的網 頁回退而不是推出瀏覽器,需要在當前Activity中處理並消費掉該Back事件。
    public boolean onKeyDown(int keyCode, KeyEvent event) {       
            if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {       
                webView.goBack();//退回到上一個頁面       
                return true;       
            }       
            return super.onKeyDown(keyCode, event);       
     }     
    
}

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM