在android中加载对话框

时间:2012-07-19 11:09:31

标签: android progressdialog

我在webview中加载对话框时遇到问题。我的对话框是dismis,但我必须再等一下才能显示完整的webview ..我想要的是,如果加载对话框是dismis webview已完成..这是我的webview代码:

progressBar = new ProgressDialog(this);
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);

final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
progressBar = ProgressDialog.show(WebViewFacebook.this, "Loading", "Please wait for a moment...");

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(); //--> this progressbar always dismis although mw webview not done ><
        }
    }

    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();
    }
});
webView.loadUrl(strurl);

1 个答案:

答案 0 :(得分:1)

请参阅以下代码

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    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.XXX");

}

然后创建类

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