[转载]webview 加载数据时显示进度条,加载完后再把进度条取消并显示内容_chenchen132_新浪博客.
webview 加载数据时显示进度条有两种.
第一种方法
这个是加载数据时显示进度条
super.onCreate(savedInstanceState); this.getWindow().requestFeature(Window.FEATURE_PROGRESS); setContentView(R.layout.main); cWebView = (WebView) findViewById(R.id.Conditions); cWebView.getSettings().setJavaScriptEnabled(true); cWebView.getSettings().setSupportZoom(true); cWebView.getSettings().setBuiltInZoomControls(true); url = "http://site.baidu.com/"; cWebView.setWebChromeClient(new WebChromeClient() { public void onProgressChanged(WebView view, int progress) { // Activity和Webview根据加载程度决定进度条的进度大小 // 当加载到100%的时候 进度条自动消失 setTitle("Loading..."); setProgress(progress * 100); } }); cWebView.loadUrl(url);
第二种方法
这个是加载数据时显示ProgressDialog
class MyWebViewClient extends WebViewClient{ @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } @Override public void onPageFinished(WebView view, String url) { if(progressBar.isShowing()){ progressBar.dismiss(); } } @Override public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { Toast.makeText(EnterListenter.this, "网页加载出错!", Toast.LENGTH_LONG); alertDialog.setTitle("ERROR"); alertDialog.setMessage(description); alertDialog.setButton("OK", new DialogInterface.OnClickListener(){ public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }); alertDialog.show(); } } protected void initWebView(){ //设计进度条 progressBar = ProgressDialog.show(EnterListenter.this, null, "正在进入网页,请稍后…"); //获得WebView组件 cWebView = (WebView) findViewById(R.id.Conditions); cWebView.getSettings().setJavaScriptEnabled(true); cWebView.getSettings().setSupportZoom(true); cWebView.getSettings().setBuiltInZoomControls(true); url = "http://site.baidu.com/"; cWebView.loadUrl(url); alertDialog = new AlertDialog.Builder(this).create(); //设置视图客户端 cWebView.setWebViewClient(new MyWebViewClient()); }