在 webview 中(进度 == 100)后进度对话框不会关闭

Progress dialog not dismiss after (progress == 100) in webview

页面加载后进度对话框没有关闭,这是我缺少的

 final ProgressDialog progressDialog = new ProgressDialog(Webview.this );
WebView webview= (WebView) findViewById(R.id.webViewID);
    webview.setWebViewClient(new WebViewClient());

    //webview.loadData(content, "text/html", "UTF-8");
    webview.loadUrl("http://hop.betfanplus.com/10000/0/mega4");

    webview.setWebChromeClient(new WebChromeClient() {
          @Override
          public void onProgressChanged(WebView view, int progress) {
             // progressDialog.show();
             ProgressDialog
                      .show(Webview.this,
                              "ProgressDialog Title",
                                  "ProgressDialog Body");

              if (progress == 100) {
                 progressDialog.dismiss();               
                 }
          }
      });

感谢任何帮助。

您仍应使用 onPageStarted 和 onPageFinished。

根据我的经验,即使网页加载失败,使用进度百分比也会达到100,这样是行不通的。

boolean loadingFinished = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    final ProgressDialog progressDialog = new ProgressDialog(Webview.this );
    progressDialog.setMessage("ProgressDialog Body");
    progressDialog.show();
    WebView webview= (WebView) findViewById(R.id.webViewID);
    webview.getSettings().setJavaScriptEnabled(true);
    webview.getSettings().setDomStorageEnabled(true);

    webview.setWebViewClient(new WebViewClient() {

        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            Toast.makeText(getApplicationContext(), description, Toast.LENGTH_SHORT).show();
        }

        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            loadingFinished = false;
        }

        public void onPageFinished(WebView view, String url) {

            loadingFinished = true;
            progressDialog.cancel();
        }

    });
    webview.setWebChromeClient(new WebChromeClient());
    webview.loadUrl("http://hop.betfanplus.com/10000/0/mega4");


}

根据我的经验,是在 oncreate 方法中声明 webview,然后使用 webview 上下文声明 progressDialog,如下所示:

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

webView = findViewById(R.id.web_view);
 progressDialog = new ProgressDialog(webView.getContext(), R.style.CustomAlertDialogStyle);

}

然后设置setWebCrhomeListener

webView.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress) {
                if (!progressDialog.isShowing()) {
                    progressDialog.show();
                }
                progressDialog.setProgress(progress);
           

        });
}

然后在onPageFinished()和onPageStarted()中控制Dialog的show()和dismiss()

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            progressDialog.dismiss();
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            progressDialog.show();
        }