加载webview后,如何使我的进度对话框解除?

时间:2010-07-19 18:19:17

标签: java android webview progressdialog dismiss

在加载webview后,我需要对代码dismiss()进行编码?

public void onCreate(Bundle savedInstanceState) { 
            super.onCreate(savedInstanceState); 
            setContentView(R.layout.main); 
            CookieSyncManager.createInstance(this);
            CookieSyncManager.getInstance().startSync();
            webview = (WebView) findViewById(R.id.webview);
            webview.setWebViewClient(new homeClient());
            webview.getSettings().setJavaScriptEnabled(true);
            webview.getSettings().setPluginsEnabled(true);
            webview.loadUrl("http://google.com");

            ProgressDialog pd = ProgressDialog.show(Home.this, "", 
                    "Loading. Please wait...", true);

        }

我试过

public void onPageFinshed(WebView view, String url){
        pd.dismiss();
    }

没用。

6 个答案:

答案 0 :(得分:16)

:○ webview.setWebViewClient(new homeClient());的 homeClient() ????

试试这个

...
...
...

webview = (WebView) findViewById(R.id.webview);
webview.setWebViewClient(new WebViewClient() {
            public boolean shouldOverrideUrlLoading(WebView view, String url) {              
                view.loadUrl(url);
                return true;
            }


          public void onPageFinished(WebView view, String url) {
                if (progressBar.isShowing()) {
                    progressBar.dismiss();
                }
            }
 webview.loadUrl("http://www.google.com");

}

<强>更新:: 这是一个很好的例子。

Android WebView and the Indeterminant Progress Solution

答案 1 :(得分:3)

没关系。

public class WordActivity extends Activity {

    private WebView webview;
    private ProgressDialog progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.webview);
        Bundle objetbunble  = this.getIntent().getExtras(); 
        webview = (WebView) findViewById(R.id.webview);

        final Activity activity = this;

        webview.getSettings().setJavaScriptEnabled(true);

        webview.setWebViewClient(new WebViewClient() {
            public boolean shouldOverrideUrlLoading(WebView view, String url) {              
                view.loadUrl(url);
                return true;
            }
            public void onLoadResource (WebView view, String url) {
                if (progressDialog == null) {
                    progressDialog = new ProgressDialog(activity);
                    progressDialog.setMessage("Chargement en cours");
                    progressDialog.show();
                }
            }
            public void onPageFinished(WebView view, String url) {
                if (progressDialog.isShowing()) {
                    progressDialog.dismiss();
                    progressDialog = null;
                }
            }
        }); 
        webview.loadUrl("http://www.example.com");
    }


}

答案 2 :(得分:3)

@Jorgesys这不是100%准确。如果页面中有多个iframe,则会有多个onPageFinished(和onPageStarted)。如果你有几个重定向,它也可能会失败。我认为这种方法可以解决所有问题:

boolean loadingFinished = true;
boolean redirect = false;

mWebView.setWebViewClient(new WebViewClient() {

   @Override
   public boolean shouldOverrideUrlLoading(WebView view, String urlNewString) {
       if (!loadingFinished) {
          redirect = true;
       }

   loadingFinished = false;
   webView.loadUrl(urlNewString);
   return true;
   }

   @Override
   public void onPageStarted(WebView view, String url) {
        loadingFinished = false;
        //SHOW LOADING IF IT ISNT ALREADY VISIBLE  
    }

   @Override
   public void onPageFinished(WebView view, String url) {
       if(!redirect){
          loadingFinished = true;
       }

       if(loadingFinished && !redirect){
         //HIDE LOADING IT HAS FINISHED
       } else{
          redirect = false; 
       }

    }
});

答案 3 :(得分:1)

您如何访问pd中的onPageFinshed()? (并且你确定它在页面加载时实际调用了吗?)

onCreate()中,尝试将pd传递给homeClient,以便homeClient可以解雇对话。

您的homeClient应如下所示:

private class homeClient extends WebViewClient {

    private ProgressDialog pd;

    public homeClient(ProgressDialog pd) {
        this.pd = pd;
    }

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

    @Override
    public void onPageFinished (WebView view, String url) {
        if (pd.isShowing()) {
            pd.dismiss();
        }
    }
}

onCreate()

ProgressDialog pd = ProgressDialog.show(Fbook.this, "", 
                "Loading. Please wait...", true);
webview.setWebViewClient(new homeClient(pd));

答案 4 :(得分:0)

  

如果您只是想在WebView中加载webPage之前显示Progress。您可以直接请求窗口功能进度,如

getWindow().requestFeature(Window.FEATURE_PROGRESS); 

之前setContentView(R.layout.blahblah);

并在onProgressChanged中显示它的进度,如

 final Activity context= this;
 webview.setWebChromeClient(new WebChromeClient() 
 {
   public void onProgressChanged(WebView webView, int progress) 
   {
     activity.setProgress(progress * 1000);
   }
 });

如果您想添加自己的 ProgressDialog,请使用WebviewClient

webView.setWebViewClient(new WebViewClient() {
    ProgressDialog rogressDialog ;
    @Override
    public void onPageStarted(WebView view, String url, Bitmap bitmap) {
        progressDialog = ProgressDialog.show(context, "Loading...", "Please wait...");//where context = YourActivity.this;
        super.onPageStarted(view, url, bitmap);
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        progressDialog .dismiss();
        super.onPageFinished(view, url);
    }
});

webView.loadUrl(url);

答案 5 :(得分:0)

wv1.setWebViewClient(new WebViewClient() {
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                Toast.makeText(Entertainment.this, description, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon)
            {
                progressDialog.show();
            }


            @Override
            public void onPageFinished(WebView view, String url) {
                progressDialog.dismiss();
                String webUrl = wv1.getUrl();

            }

        });

    wv1.loadUrl(url);

以上代码将显示您导航到网络视图中的每个传入链接的进度对话框。