webview上的Android进度条出现两次

时间:2012-03-26 05:44:31

标签: android webview progress-bar webviewclient webchromeclient

我正在尝试使用progress bar为Android创建一个简单的应用。一切正常。但是,这里有两个问题

1)启动应用程序后,我可以看到两次加载进度。

2)如何在初始页面加载完成后禁用进度条。我不想在每次点击时显示进度条..

这是我的代码

package com.mycom.jquery;
import android.app.Activity;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class HelloWebView extends Activity {

WebView webview;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.getWindow().requestFeature(Window.FEATURE_PROGRESS);

    setContentView(R.layout.main);
    getWindow().setFeatureInt(Window.FEATURE_PROGRESS,
            Window.PROGRESS_VISIBILITY_ON);

    webview = (WebView) findViewById(R.id.webview);
    webview.getSettings().setJavaScriptEnabled(true);

    webview.setWebViewClient(new WebViewClient() {

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

    });
    final Activity activity = this;

    final ProgressDialog progressDialog = new ProgressDialog(activity);
    progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    progressDialog.setMessage("Loading...Jquery.com");
    progressDialog.setCancelable(true);

    webview.loadUrl("http://www.jquery.com");

    // WebChromeClient give progress etc info
    webview.setWebChromeClient(new WebChromeClient() {

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

}

}

3 个答案:

答案 0 :(得分:2)

尝试拨打

progressDialog.show();

以外的

onProgressChanged(WebView view, int progress)

答案 1 :(得分:1)

当进度变为100时设置标志,然后如果该标志为true,则立即在onProgressChanged实施中返回。

答案 2 :(得分:0)

this.getWindow().requestFeature(Window.FEATURE_PROGRESS);

尝试删除它。从你的代码。