1. 程式人生 > >android webview 載入顯示網頁載入進度條的例子

android webview 載入顯示網頁載入進度條的例子

1 增加許可權

<uses-permission android:name="android.permission.INTERNET"/>

2 主Activity

package com.example.webview;

import android.app.Activity;
import android.graphics.Bitmap;
import android.net.http.SslError;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

public class MainActivity extends Activity {

 private WebView webView;

 private ProgressBar progressBar;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  webView = (WebView) findViewById(R.id.webView);
  progressBar = (ProgressBar) findViewById(R.id.progressBar1);
  progressBar.setMax(100);
  WebSettings settings = webView.getSettings();
  settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
  // 使得webview支援js
  settings.setJavaScriptEnabled(true);
  webView.requestFocus();
  webView.loadUrl("

https://www.baidu.com/");
  // 覆蓋WebView預設使用第三方系統或者預設瀏覽器開啟網頁的行為,使網頁使用webview開啟
  webView.setWebViewClient(new WebViewClient() {
   @Override
   public boolean shouldOverrideUrlLoading(WebView view, String url) {
    view.loadUrl(url);
    return true;
   }

   @Override
   // 當遇到https請求的時候進行的處理
   public void onReceivedSslError(WebView view,
     SslErrorHandler handler, SslError error) {
    handler.proceed();
   }

   @Override
   public void onPageFinished(WebView view, String url) {
    // TODO Auto-generated method stub
    super.onPageFinished(view, url);
    progressBar.setProgress(100);
    progressBar.setVisibility(ProgressBar.INVISIBLE);
   }

   @Override
   public void onPageStarted(WebView view, String url, Bitmap favicon) {
    // TODO Auto-generated method stub
    super.onPageStarted(view, url, favicon);
    progressBar.setVisibility(ProgressBar.VISIBLE);
    progressBar.setProgress(0);
   }

  });
  webView.setWebChromeClient(new WebChromeClient() {

   @Override
   public void onProgressChanged(WebView view, int newProgress) {
    if (newProgress == 100) {
     progressBar.setVisibility(ProgressBar.INVISIBLE);
    } else {
     Log.e("MyTag", "載入進度" + newProgress);
    }
    progressBar.setProgress(newProgress);
    super.onProgressChanged(view, newProgress);
   }

  });
 }

 @Override
 public boolean onKeyDown(int keyCode, KeyEvent event) {
  if (keyCode == KeyEvent.KEYCODE_BACK) {
   if (webView.canGoBack()) {
    webView.goBack();
    return true;
   }
  }
  return super.onKeyDown(keyCode, event);
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.main, menu);
  return true;
 }

}

3 佈局檔案

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ProgressBar
        android:id="@+id/progressBar1"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <WebView
        android:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
   
</LinearLayout>