如何在为我的代码加载网页时获取Progressdialog?

时间:2016-05-20 08:10:56

标签: android webview

import android.app.Activity;    
import android.content.Context;    
import android.content.DialogInterface;    
import android.os.Bundle;    
import android.view.Menu;    
import android.view.MenuItem;    
import android.webkit.WebView;    
import android.app.AlertDialog;    
import android.app.ProgressDialog;    
import android.app.Activity;    
import android.os.Bundle;
import android.webkit.WebView;    
import android.webkit.WebViewClient;    


public class MainActivity extends Activity {   

    private WebView webView;    


    protected void onCreate(Bundle savedInstanceState) {    
        super.onCreate(savedInstanceState);    
        setContentView(R.layout.activity_main);   
        webView = (WebView) findViewById(R.id.book);   
        webView.getSettings().setBuiltInZoomControls(true);    
        webView.getSettings().setLoadWithOverviewMode(true);    
        webView.getSettings().setUseWideViewPort(true);    
        webView.getSettings().setSupportZoom(true);  
        webView.setWebViewClient(new WebViewClient() {   
            @Override   
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {   
                super.onReceivedError(view, errorCode, description, failingUrl);   
                loadError();    
            }    
        });
        webView.loadUrl("http://www.google.com");

    }



    private void loadError() {    
        String html = "<html><body><table width=\"100%\" height=\"100%\" 
border=\"0\" cellpadding=\"0\" cellspacing=\"0\">"

                + "<tr>"

                + "<td><div align=\"center\"><font color=\"red\" size=\"20pt\"> 
No internet Connection </font></div></td>"    
                + "</tr>" + "</table><html><body>";    
        System.out.println("html " + html);   
        String base64 = android.util.Base64.encodeToString(html.getBytes(),android.util.Base64.DEFAULT);            webView.loadData(base64, "text/html; charset=utf-8", "base64");    
        System.out.println("loaded html");    
    }






<?xml version="1.0" encoding="utf-8"?>    
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
    android:layout_width="fill_parent"    
    android:layout_height="fill_parent"    
    android:orientation="vertical" >



   <WebView      
    android:id="@+id/book"    
    android:layout_width="fill_parent"    
    android:layout_height="fill_parent"    
/>       

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

使用以下代码:

    new WebViewClient(){
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);

            // Show progress dialog
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);

            // Hide progress dialog
        }
    };