在webview上显示进度对话框

Rem*_*van 8 android

在我的Android应用程序中,我打开webview.我想隐藏正在加载的URL,因此默认窗口进度条对我不起作用.

有什么办法可以在webview上添加进度对话框.

我使用以下代码.

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

     final Activity activity = this; 

     mWebView.setWebChromeClient(new WebChromeClient(){ 

     public void onProgressChanged(WebView view, int progress) { 
     activity.setTitle("Loading..."); 
     activity.setProgress(progress * 100); 
     if(progress == 100) 
     activity.setTitle("My title"); 
     } 
     }); 

     mWebView.loadUrl(Url);
     mWebView.setWebViewClient(new HelloWebViewClient());
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
Run Code Online (Sandbox Code Playgroud)

}

private class HelloWebViewClient extends WebViewClient {   
    ProgressDialog MyDialog=new ProgressDialog(context);

    public boolean shouldOverrideUrlLoading(WebView view, String url) {    
        MyDialog.show();  
        view.loadUrl(url);  
        return true;  
         }
Run Code Online (Sandbox Code Playgroud)

请分享您宝贵的建议.

提前致谢 :)

Hak*_*kan 29

如果您通过"android:theme ="@ android:style/Theme.NoTitleBar""从清单中隐藏应用程序标题,那么您将看不到此代码在标题中显示的进度条:

    this.getWindow().requestFeature(Window.FEATURE_PROGRESS);               
    setContentView(R.layout.main);

// Makes Progress bar Visible
getWindow().setFeatureInt( Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);
Run Code Online (Sandbox Code Playgroud)

所以你可以在onCreate()里面用这样的对话框显示进度条:

final Activity activity = this;

final ProgressDialog progressDialog = new ProgressDialog(activity);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setCancelable(false);

browser.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();
    }
});
Run Code Online (Sandbox Code Playgroud)