【问题标题】:how to display progress while loading a url to webview in android?如何在android中将url加载到webview时显示进度?
【发布时间】:2012-07-02 13:38:44
【问题描述】:

我正在将 url 加载到 webview 中:

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

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

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

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

【问题讨论】:

标签: android webview


【解决方案1】:

为您的 WebView 设置一个 WebViewClient,在您的 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>

【讨论】:

  • 感谢你的好例子,你拯救了我的一天!
【解决方案2】:

您将不得不覆盖 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();
        }
    });

【讨论】:

    【解决方案3】:

    查看示例代码。对你有帮助。

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

    谢谢。

    【讨论】:

    • 将 ProgressBar 的可见性设置为 GONE 是错误的 - 必须将视图从视图堆栈中删除。
    【解决方案4】:

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

    你需要实现onPageStarted(在此处显示)和onPageFinished(在此处关闭)这两个方法。

    有关此主题的更多指导可以在 Google 的 WebView tutorial 中找到

    【讨论】:

      【解决方案5】:
        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();
          }
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-07-21
        • 2011-09-29
        相关资源
        最近更新 更多