Android中如何判別WebView載入完畢並使用ProgressDialog進行提示

來源:互聯網
上載者:User

最近在搞廣告平台的一些東西,看了一下別的廣告平台的jar包,基本上都會有一個Activity用來顯示web網頁,用到了一個用來顯示網頁的組件--WebView。但是其網頁的載入進度就不受我們控制了,所以當網頁資料很多時就可能會很慢才會顯示,這樣的使用者體驗不好,如果能夠有一個進度提示就好些了,在網上搜到了一篇博文,用來解決此問題。所以發出來供大家參考。
 
當然了這篇博文其實相當於轉載,因為我沒怎麼添加新的內容,慚愧啊。
 
首先貼上我的,如下:




載入完成後的圖片:





代碼很少,基本是一個方法的事,具體如下:

[java] import android.app.Activity; 
import android.app.ProgressDialog; 
import android.os.Bundle; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 
 
public class WebViewFinishedActivity extends Activity { 
    /** Called when the activity is first created. */ 
     
    WebView web = null; 
    ProgressDialog dialog = null; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
 
        web = (WebView)findViewById(R.id.webView1); 
         
        if(web != null) 
        { 
            web.setWebViewClient(new WebViewClient() 
            { 
                @Override 
                public void onPageFinished(WebView view,String url) 
                { 
                    dialog.dismiss(); 
                } 
            }); 
             
            loadUrl("http://www.bkjia.com"); 
        } 
         
    } 
     
     
    public void loadUrl(String url) 
    { 
        if(web != null) 
        { 
            web.loadUrl(url); 
            dialog = ProgressDialog.show(this,null,"頁面載入中,請稍後.."); 
            web.reload(); 
        } 
    } 
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class WebViewFinishedActivity extends Activity {
    /** Called when the activity is first created. */
 
 WebView web = null;
 ProgressDialog dialog = null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        web = (WebView)findViewById(R.id.webView1);
       
        if(web != null)
        {
         web.setWebViewClient(new WebViewClient()
         {
          @Override
          public void onPageFinished(WebView view,String url)
          {
           dialog.dismiss();
          }
         });
         
         loadUrl("http://www.bkjia.com");
        }
       
    }
   
   
    public void loadUrl(String url)
    {
     if(web != null)
     {
      web.loadUrl(url);
      dialog = ProgressDialog.show(this,null,"頁面載入中,請稍後..");
      web.reload();
     }
    }
xml布局也貼上吧,省的在自己碼了:

[html] <?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="@+id/webView1"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"></WebView> 
 
</LinearLayout> 
<?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="@+id/webView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"></WebView>

</LinearLayout>

 

開發提示:
1 調用WebView的setWebViewClient方法,為其設定載入頁面的Client。
2 為該Client重載函數onPageFinished,並在裡面調用ProgressDialog的dismiss函數。
3 務必在AndroidManifest.xml中加入

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

並注意放的位置。
4 由於在多個函數中使用了ProgressDialog,所以將其設計類的變數。
5 代碼下載:http://www.bkjia.com/uploadfile/2012/0410/20120410103313842.zip
 

 摘自 agods--足跡
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.