使用对话框进度和webchrome客户端显示webview

时间:2012-07-31 04:15:30

标签: android webview progressdialog android-progressbar

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

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 个答案:

答案 0 :(得分: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");

答案 1 :(得分:1)

当页面为progressbar时,请在下面的代码中查看显示loading到webview的内容。

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);  
    }  

}

答案 2 :(得分: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");

答案 3 :(得分: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);
    }
}