如何在WebView中加载HTML页面时显示进度对话框

时间:2012-02-07 05:40:55

标签: android webview progressdialog

我正在使用Web视图显示html页面,我想在页面加载之前显示进度对话框。完成后,对话框必须消失。我已经使用了AsyncTask,但对话框没有显示。请参阅下面的代码:

 class DownloadAysnc extends AsyncTask<String, String, Void>
   {
    ProgressDialog progressDialog;
        @Override
          protected void onPreExecute() {
          super.onPreExecute();
          progressDialog = ProgressDialog.show(OverView.this, "", "Please Wait ...");
        }

        @Override
          protected Void doInBackground(String... arg0) {
          webView.loadUrl("http://marico.com/html/investor/overview.php");
        return null;
       }

        @Override
          protected void onPostExecute(Void result){
          super.onPostExecute(result);
          progressDialog.dismiss();
       }
   }

如果我借助谷歌文档显示网页,则会显示HTML标记,但不会显示页面。以下是该代码:

String url = "http://google.co.in/";
String googleDocsUrl = "http://docs.google.com/viewer?url="+url;
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(googleDocsUrl ), "text/html");
startActivity(intent);

this.myWebView.loadUrl(googleDocsUrl);  

有人可以帮我这个吗?

4 个答案:

答案 0 :(得分:13)

使用此代码:

webView.setWebViewClient(new WebViewClient() {
    ProgressDialog prDialog;
    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        prDialog = ProgressDialog.show(Activity.this, null, "loading, please wait...");
        super.onPageStarted(view, url, favicon);
    }

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

webView.loadUrl(url);

答案 1 :(得分:3)

  v.setWebChromeClient(new WebChromeClient(){
      @Override
      public void onProgressChanged(WebView view, int newProgress) {
          WebView v=(WebView)findViewById(R.id.wv);
          //Toast.makeText(mContext, v.getUrl() + newProgress +"uploading...", Toast.LENGTH_SHORT).show();
          ProgressBar s=(ProgressBar)findViewById(R.id.progressBar1);
          s.setMax(100);
          s.setProgress(newProgress);
          if(newProgress==100){                        
              v.setVisibility(0);
              //Toast.makeText(mContext, "upload finished...", Toast.LENGTH_SHORT).show();
         }else{
              v.setVisibility(8);
              //Toast.makeText(mContext, "uploading...", Toast.LENGTH_SHORT).show();
          }
        }
    });

答案 2 :(得分:2)

您可以在WebView的标题栏中显示Progress的{​​{1}}。 Here是在WebView的{​​{1}}栏中显示Progress Status的完整示例。

答案 3 :(得分:1)

您可以尝试以下代码,

progDailog = ProgressDialog.show(loginAct,"Process ", "please wait....",true,true);

new Thread ( new Runnable()
{
     public void run()
     {
      // your code goes here
     }
}).start();

 Handler progressHandler = new Handler() 
 {

     public void handleMessage(Message msg1) 
     {
         progDailog.dismiss();
     }
 }