WebView中的Android进度条

时间:2013-10-25 08:08:37

标签: android webview

最近要创建一个webView我正在使用webViewClient。 mWebView.setWebViewClient(new WebViewClient());

但我需要实现一个进度条。当用户单击链接时,将显示此进度条。页面完成后,将隐藏进度条并显示webView。关于thisthis,我添加了一个WebChromeClient。但它会加载第一个URL,但是当我单击我的网页内的一个按钮时,会打开一个对话框,要求打开哪个应用程序的URL。

我读到我应该覆盖shouldOverrideUrlLoading()方法。但是我收到一条错误,“{OnrideUrlLoading”无法覆盖WebChromeClient

如果你能给出一个带有progressbar和webView的例子,我会很高兴,也可以在同一个webView中打开新的URL。

public class WebActivity extends Activity {
    WebView mWebView;
    ProgressBar mProgress;
    Context mContext;
    ProgressBar mProgressBar;

   @Override
   public void onCreate(Bundle savedInstanceState) {
      this.mContext = getApplicationContext();

      super.onCreate(savedInstanceState);
      setContentView(R.layout.web);

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

      mWebView.setWebChromeClient(new myWebChromeClient());
      mWebView.getSettings().setJavaScriptEnabled(true);
      mWebView.getSettings().setUserAgentString("myApp");

      mProgressBar = (ProgressBar) findViewById(R.id.webProgressBar);
      mProgressBar.setMax(100);
  }

  public class myWebChromeClient extends WebChromeClient {
      @Override
      public boolean shouldOverrideUrlLoading(WebView view, String url) {
          view.loadUrl(url);
          return false;
      }
      @Override
      public void onProgressChanged(WebView view, int newProgress) {
          //WebActivity.this.setValue(newProgress);
          super.onProgressChanged(view, newProgress);
      }
  }
}

3 个答案:

答案 0 :(得分:3)

  try with this code 
  webView = (WebView) view.findViewById(R.id.transcationwebview);
            progressdialog = ProgressDialog.show(mContext, "",
                    mContext.getString(R.string.please_wait));
            progressdialog.setCancelable(true);
            progressdialog.setOnCancelListener(new OnCancelListener() {

                @Override
                public void onCancel(DialogInterface dialog) {
                    webView.stopLoading();
                    // webView.clearView();

                }
            });

            webView.getSettings().setJavaScriptEnabled(true);
            webView.setWebChromeClient(new MyChromeClient());
            webView.setWebViewClient(new WebViewClient() {
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    // TODO Auto-generated method stub
                    view.loadUrl(url);
                    return super.shouldOverrideUrlLoading(view, url);
                }

                @Override
                public void onPageFinished(WebView view, String url) {
                    // TODO Auto-generated method stub
                    super.onPageFinished(view, url);
                    if (progressdialog != null && progressdialog.isShowing()) {
                        progressdialog.dismiss();
                    } else {


                    }

                }

                @Override
                public void onPageStarted(WebView view, String url, Bitmap favicon) {
                    // TODO Auto-generated method stub
                    super.onPageStarted(view, url, favicon);
                }

                @Override
                public void onReceivedError(WebView view, int errorCode,
                        String description, String failingUrl) {
                    // TODO Auto-generated method stub

                    super.onReceivedError(view, errorCode, description, failingUrl);

                }
            });

            webView.loadUrl("url");
            webView.getSettings().setBuiltInZoomControls(true);



public class MyChromeClient extends WebChromeClient {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            try {

                if (progressdialog.isShowing()) {
                    progressdialog.setMessage(getString(R.string.loading)
                            + newProgress + " %");

                } else {
                    /*
                     * webView.stopLoading(); webView.clearView();
                     */
                }
            } catch (Throwable e) {

                e.printStackTrace();
            }
        }
    }

答案 1 :(得分:1)

您可以extends WebViewClient代替WebChromeClient并覆盖onPageStarted以显示ProgressBar并在onPageReceived

中将其关闭

答案 2 :(得分:0)

我在活动中做了一些更改。

使用 WebViewClient 代替 WebChromeClient

并使用 ProgressDialog 代替 ProgressBar

public class WebActivity extends Activity {
    WebView mWebView;
    Context mContext;
    ProgressDialog mProgressBar;
    private static final int DIALOG2_KEY = 1;

   @Override
   public void onCreate(Bundle savedInstanceState) {
      this.mContext = getApplicationContext();
      super.onCreate(savedInstanceState);
      setContentView(R.layout.web);
      mWebView = (WebView) findViewById(R.id.webview);
      mWebView.setWebViewClient(new MyWebChromeClient());
      mWebView.getSettings().setBuiltInZoomControls(true);
      showDialog(DIALOG2_KEY);  }

    @Override
        protected void onResume() {
            super.onResume();
            mWebView.loadUrl("YOUR_URL");
              }

   private final class MyWebChromeClient extends WebViewClient {
        @Override
        public void onPageFinished(WebView view, String url) {
            dismissDialog(DIALOG2_KEY);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (url.startsWith("file")) {
                return false;
            } else{
                view.loadUrl(url);
                return true;
            }
        }
    }

   @Override
    protected Dialog onCreateDialog(int id) {
        switch (id)
        {
        case DIALOG2_KEY:
        {
            mProgressBar.setMessage("Loading");
            mProgressBar.setIndeterminate(true);
            mProgressBar.setCancelable(false);
            return mProgressBar;
        }
        }
        return null;
    }
}

希望这能帮到你