0

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

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

我读到我应该覆盖shouldOverrideUrlLoading()方法。但我得到一个错误,“shouldOverrideUrlLoading”不能被覆盖WebChromeClient

如果你能给出一个有进度条和 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);
      }
  }
}
4

3 回答 3

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();
            }
        }
    }
于 2013-10-25T08:19:54.703 回答
1

你可以extends WebViewClient代替WebChromeClient并覆盖它onPageStarted来显示ProgressBar和关闭它onPageReceived

于 2013-10-25T08:15:39.267 回答
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;
    }
}

希望这对你有帮助

于 2013-10-25T08:19:27.323 回答