如何显示进度,同时加载一个url到android的webview?

我正在加载url到webview中:

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

加载url需要一些时间,在此期间它显示一个空白屏幕。 我想在加载url时显示一个进度对话框:

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

但是,上面的代码是不行的。 如果有任何想法,请帮助。 提前致谢。

设置一个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> 

你将不得不搭上onPageStarted和onPageFinishedcallback

 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(); } }); 

查看示例代码。 它帮助你。

  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); } } 

谢谢。

您需要通过扩展WebViewClient类来为您的WebView设置自己的WebViewClient 。

你需要实现两个方法onPageStarted(显示在这里)和onPageFinished(closures这里)。

有关此主题的更多指导可以在Google的WebView教程中find

  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(); } });