点击用于Webview加载的Android进度对话框 - 空白屏幕

时间:2013-05-07 12:17:19

标签: android android-webview progressdialog

我尝试在Webview中加载内容时显示progressDialog。完成后,progressDialog将被解除。

private ProgressDialog progressDialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
    webView = (WebView) findViewById(R.id.webView);
    webView.setWebViewClient(new WebViewClient(){

        @Override
        public void onLoadResource (WebView view, String url) {
            //show progress bar
            if (progressDialog == null) {
                progressDialog = new ProgressDialog(activity);
                progressDialog.setMessage("Loading please wait...");
                progressDialog.show();
            }   
        }

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

    });
}

我有2个测试设备Android 2.3.6和Android 4.0。在Android 2.3.6中,完全没问题。对于Android 4.0平板电脑,在显示进度对话框时,点击它将隐藏或删除progressDialog并将屏幕留空。

我的progressDialog有什么问题?

由于

2 个答案:

答案 0 :(得分:1)

将此作为您的webView客户端

 webView.setWebViewClient(new WebViewClient(){

            @Override
            public void onLoadResource (WebView view, String url) {
                //show progress bar
                if (progressDialog == null) {
                    progressDialog = new ProgressDialog(MainActivity.this);
                    progressDialog.setMessage("Loading please wait...");
                    progressDialog.show();
                    progressDialog.setCanceledOnTouchOutside(false);
                    progressDialog.setCancelable(false);
                }   
            }

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

        }); 

答案 1 :(得分:0)

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

        setContentView(R.layout.main);

        WebView webview = (WebView) findViewById(R.id.webView1);
        webview.loadUrl("http://google.com");
        final ProgressDialog pd = ProgressDialog.show(this, "please wait", "Loading...");
        webview.getSettings().setBuiltInZoomControls(true);
        webview.setWebViewClient(new WebViewClient() {
                    public boolean shouldOverrideUrlLoading(WebView view, String url) {              
                        view.loadUrl(url);
                        return true;
                    }


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

        });
    }