1

我的应用程序有 webview,我添加了显示加载的进度对话框,直到 web 完成加载。我在谷歌搜索并得到了很好的解决方案

wv.setWebChromeClient(new MyWebChromeClient());
wv.setWebViewClient(new MyWebViewClient());
wv.loadUrl("http://www.google.com"); 

     private class MyWebViewClient extends WebViewClient {
        @Override
         public boolean shouldOverrideUrlLoading(WebView view, String url) 
        {
            view.loadUrl(url);
            return true;
        }

        @Override
     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;
                finish();
             }
         });
            alertdialog.show();
     }
    }


    private class MyWebChromeClient extends WebChromeClient {

        @Override
        public void onProgressChanged(WebView view, int progress) {
            progressDialog.show();
            progressDialog.setProgress(0);
            activity.setProgress(progress * 1000);

            progressDialog.incrementProgressBy(progress);

            if(progress == 100 && progressDialog.isShowing())
                progressDialog.dismiss();
        }

    }

但我的问题是,如何让progressdialog在我启动它后只出现一次,因为现在如果我点击像图片(在google.com中)这样的例子,进度对话框会再次出现。

4

4 回答 4

4
this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
            setContentView(R.layout.info);

            mProgressDialog = new ProgressDialog(this);
            mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            mProgressDialog.setMessage("Please wait...");
            mProgressDialog.setCancelable(false);
            mProgressDialog.setMax(100);
            mProgressDialog.show();

            WebView webView = (WebView) findViewById(R.id.webview);
            webView.setVisibility(View.VISIBLE);
            webView.getSettings().setJavaScriptEnabled(true);

            webView.setWebChromeClient(new WebChromeClient() {
                public void onProgressChanged(WebView view, final int progress)
                {
                    activity.setTitle("Loading...");
                    activity.setProgress(progress * 100);
                    mProgressDialog.setProgress(progress );




                    if(progress == 100)
                    {
                        activity.setTitle(R.string.app_name);
                        mProgressDialog.dismiss();
                    }

                }
            });

            webView.setWebViewClient(new WebViewClient() {
                @Override
                public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
                {
                }

                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url)
                {
                    view.loadUrl(url);
                    return true;
                }
            });

            webView.loadUrl("http://www.google.com");
于 2012-07-31T05:26:03.517 回答
1

检查以下代码以progressbar在页面为loading.

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
    setContentView(R.layout.newsdisplay);
    getWindow().setFeatureInt( Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON); 


    newswebview = (WebView) findViewById(R.id.news_webView);
    newswebview.setWebViewClient(new WebViewClient());
    WebSettings webSettings = newswebview.getSettings();
    webSettings.setJavaScriptEnabled(true);

    newswebview.getSettings().setSupportZoom(true);       //Zoom Control on web (You don't need this 
     //if ROM supports Multi-Touch      
    newswebview.getSettings().setBuiltInZoomControls(true); //Enable Multitouch if supported by ROM


    newswebview.loadUrl(url);


    final Activity MyActivity = this;

newswebview.setWebChromeClient(new WebChromeClient() {
    public void onProgressChanged(WebView view, int progress) 
       {

        //Make the bar disappear after URL is loaded, and changes string to Loading...
        MyActivity.setTitle("  Loading...");
        MyActivity.setProgress(progress * 100); //Make the bar disappear after URL is loaded

        // Return the app name after finish loading
           if(progress == 100)
              MyActivity.setTitle(R.string.app_name);

    }
});

}


 @Override  
    public boolean onKeyDown(int keyCode, KeyEvent event)   
    {  
        if ((keyCode == KeyEvent.KEYCODE_BACK) && newswebview.canGoBack()) {  
            newswebview.goBack();  
            return true;  
        }  
        return super.onKeyDown(keyCode, event);  
    }  

}
于 2012-07-31T04:44:10.683 回答
1

//使用此代码在 webview 中显示进度

this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
            setContentView(R.layout.info);
            WebView webView = (WebView) findViewById(R.id.webview);
            webView.setVisibility(View.VISIBLE);
            webView.getSettings().setJavaScriptEnabled(true);

            webView.setWebChromeClient(new WebChromeClient() {
                public void onProgressChanged(WebView view, int progress)
                {
                    activity.setTitle("Loading...");
                    activity.setProgress(progress * 100);

                    if(progress == 100)
                        activity.setTitle(R.string.app_name);
                }
            });

            webView.setWebViewClient(new WebViewClient() {
                @Override
                public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
                {
                }

                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url)
                {
                    view.loadUrl(url);
                    return true;
                }
            });

            webView.loadUrl("http://www.google.com");
于 2012-07-31T04:46:11.757 回答
1

你可以试试下面的代码

    view = (WebView) findViewById(R.id.WebView01);
    view.setBackgroundColor(0);  // set the background transparent 

    progressDialog = ProgressDialog.show(view.getContext(), "Loading...", "Loading...");
    progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER    );

    view.setWebChromeClient(new MyWebChromeClient());
    view.getSettings().setJavaScriptEnabled(true);
    view.setWebViewClient(new MyWebViewClient());
    view.loadUrl("http://www.xxx.com/web/apps/jots.do");

然后创建类

private class MyWebViewClient extends WebViewClient {
    @Override
     public boolean shouldOverrideUrlLoading(WebView view, String url)      
    {
        view.loadUrl(url);
        return true;
    }
}

private class MyWebChromeClient extends WebChromeClient {

    @Override
    public void onProgressChanged(WebView view, int newProgress) {
        if(progressDialog!=null){
            if ( newProgress >=80 ) {
                progressDialog.dismiss();
            } else {
                progressDialog.setMessage(newProgress + " % loaded");
            }
        }
        super.onProgressChanged(view, newProgress);
    }
}
于 2012-07-31T05:14:19.887 回答