2012-07-02 61 views
59

我加載網址爲網頁流量:如何在android中將web url加載到webview時顯示進度?

WebView webview=(WebView)findViewById(R.id.webview); 
webview.loadUrl(url); 

它需要一些時間來加載URL,在此期間,它顯示了一個空白屏幕。我想在URL加載時顯示進度對話框:

ProgressDialog dialog = ProgressDialog.show(this, "HI","Loading......", true); 

但是,上面的代碼是不工作的。如果有任何想法,請幫助。提前致謝。

+0

可能重複:http://stackoverflow.com/questions/3149216/how-to-listen-for-a-webview- finishing-loading-a-url – LarsH

回答

112

中找到設置了WebViewClient到您的WebView,在你開始你的進度對話框onCreate()方法的關閉它,當頁面中onPageFinished(WebView view, String url)

import android.app.Activity; 
import android.app.AlertDialog; 
import android.app.ProgressDialog; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.net.Uri; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.Window; 
import android.webkit.WebSettings; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 
import android.widget.Toast; 

public class Main extends Activity { 
    private WebView webview; 
    private static final String TAG = "Main"; 
    private ProgressDialog progressBar; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     requestWindowFeature(Window.FEATURE_NO_TITLE); 

     setContentView(R.layout.main); 

     this.webview = (WebView)findViewById(R.id.webview); 

     WebSettings settings = webview.getSettings(); 
     settings.setJavaScriptEnabled(true); 
     webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY); 

     final AlertDialog alertDialog = new AlertDialog.Builder(this).create(); 

     progressBar = ProgressDialog.show(Main.this, "WebView Example", "Loading..."); 

     webview.setWebViewClient(new WebViewClient() { 
      public boolean shouldOverrideUrlLoading(WebView view, String url) { 
       Log.i(TAG, "Processing webview url click..."); 
       view.loadUrl(url); 
       return true; 
      } 

      public void onPageFinished(WebView view, String url) { 
       Log.i(TAG, "Finished loading URL: " +url); 
       if (progressBar.isShowing()) { 
        progressBar.dismiss(); 
       } 
      } 

      public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { 
       Log.e(TAG, "Error: " + description); 
       Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show(); 
       alertDialog.setTitle("Error"); 
       alertDialog.setMessage(description); 
       alertDialog.setButton("OK", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int which) { 
         return; 
        } 
       }); 
       alertDialog.show(); 
      } 
     }); 
     webview.loadUrl("http://www.google.com"); 
    } 
} 
加載完畢

您的main.xml佈局

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
    <WebView android:id="@string/webview" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" /> 
</LinearLayout> 
+1

謝謝你的好例子,你救了我的一天! – lakshman

1

您需要通過擴展WebViewClient類來爲WebView設置自己的WebViewClient。

你需要在onPageStarted(顯示在這裏)和onPageFinished(在這裏消除)實現這兩個方法。

此主題的更多指導可以在谷歌的WebView tutorial

3

退房的SAMPL e代碼。它可以幫助你。

private ProgressBar progressBar; 
progressBar=(ProgressBar)findViewById(R.id.webloadProgressBar); 
WebView urlWebView= new WebView(Context); 
    urlWebView.setWebViewClient(new AppWebViewClients(progressBar)); 
         urlWebView.getSettings().setJavaScriptEnabled(true); 
         urlWebView.loadUrl(detailView.getUrl()); 

public class AppWebViewClients extends WebViewClient { 
    private ProgressBar progressBar; 

    public AppWebViewClients(ProgressBar progressBar) { 
     this.progressBar=progressBar; 
     progressBar.setVisibility(View.VISIBLE); 
    } 
    @Override 
    public boolean shouldOverrideUrlLoading(WebView view, String url) { 
     // TODO Auto-generated method stub 
     view.loadUrl(url); 
     return true; 
    } 

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

謝謝。

+0

將ProgressBar的可見性設置爲GONE僅僅是錯誤的 - 視圖不得不被視爲從View-Stack中移除。 – Tim

8

你將不得不在乘坐onPageStarted和onPageFinished回調

mWebView.setWebViewClient(new WebViewClient() { 

     public void onPageStarted(WebView view, String url, Bitmap favicon) { 
      if (progressBar!= null && progressBar.isShowing()) { 
       progressBar.dismiss(); 
      } 
      progressBar = ProgressDialog.show(WebViewActivity.this, "Application Name", "Loading..."); 
     } 

     public boolean shouldOverrideUrlLoading(WebView view, String url) { 
      view.loadUrl(url); 

      return true; 
     } 

     public void onPageFinished(WebView view, String url) { 
      if (progressBar.isShowing()) { 
       progressBar.dismiss(); 
      } 
     } 

     public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { 
      alertDialog.setTitle("Error"); 
      alertDialog.setMessage(description); 
      alertDialog.setButton("OK", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 
        return; 
       } 
      }); 
      alertDialog.show(); 
     } 
    }); 
-1
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { 
     alertDialog.setTitle("Error"); 
     alertDialog.setMessage(description); 
     alertDialog.setButton("OK", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int which) { 
       return; 
      } 
     }); 
     alertDialog.show(); 
    } 
}); 
相關問題