如何在webview加载Android网页之前添加微调加载程序(NOT PROGRESS BAR)?

时间:2014-08-12 06:56:09

标签: android webview dialog spinner loader

![在此处输入图片说明] [1]

我在Alert Dialog中有一个webview。如何在加载webview之前显示加载程序微调器(NOT PROGRESS BAR),然后在加载网页后隐藏它?以下是我的代码。这里我没有使用任何xml for webview,它动态生成简单的webview:

     progress = new ProgressDialog(activity);
            progress.setMessage("Please wait......");
            progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            progress.setIndeterminate(true);
            progress.show();

            WebView wv = new WebView(activity);
            //wv.loadUrl("http:\\www.google.com");
              wv.getSettings().setJavaScriptEnabled(true);
              wv.getSettings().setBuiltInZoomControls(true);
              wv.getSettings().setSupportZoom(true);
              wv.getSettings().setUseWideViewPort(true);
              wv.setClickable(true);
              wv.setInitialScale(1);
              wv.loadUrl("https://www.yahoo.com");


            wv.setOnKeyListener(new OnKeyListener()
            {
                @Override
                public boolean onKey(View v, int keyCode, KeyEvent event)
                {
                    if(event.getAction() == KeyEvent.ACTION_DOWN)
                    {
                        WebView webView = (WebView) v;

                        switch(keyCode)
                        {
                            case KeyEvent.KEYCODE_BACK:
                                if(webView.canGoBack())
                                {
                                    webView.goBack();
                                    return true;
                                }
                                break;
                        }
                    }

                    return false;
                }
            });

            wv.setWebViewClient(new WebViewClient() {


                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    // view.loadUrl(url);
                    // return true;
                    Log.d(TAG, "shouldOverrideUrlLoading");

                    String cleanUrl = url;
                    if (url.contains("?")) {
                        // remove the query string
                        cleanUrl = url.substring(0, url.indexOf("?"));
                    }

                    if (cleanUrl.endsWith("pdf")) {
                        Intent intent = new Intent(Intent.ACTION_VIEW);
                        intent.setDataAndType(Uri.parse(url), "application/pdf");
                        try {
                            Uri uriUrl = Uri.parse(cleanUrl);
                            Intent intentUrl = new Intent(Intent.ACTION_VIEW,
                                    uriUrl);
                            view.getContext().startActivity(intentUrl);
                            return true;

                        } catch (Exception e) {
                            System.out.println(e);
                            Toast.makeText(context, "No PDF Viewer Installed",
                                    Toast.LENGTH_LONG).show();
                        }
                    } else {
                        view.loadUrl(url);
                    }
                    return true;
                }

                @Override
                public void onPageFinished(WebView view, String url) {
                    // TODO Auto-generated method stub
                    if (progress.isShowing()) {
                        progress.dismiss();
                    }
                    Log.d(TAG, "onPageFinished");

                }

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

            builder.setView(wv);
            builder.show();
            return false;

1 个答案:

答案 0 :(得分:0)

请尝试这种方式,希望这有助于您解决问题。

progressBar.setVisibility(View.VISIBLE);
wv.setWebChromeClient(new WebChromeClient() {
    public void onProgressChanged(WebView view, int progress) {
       if(progress==100){
           progressBar.setVisibility(View.GONE);
       }
    }
});