我无法阻止ProgressDialog Android

时间:2012-08-16 12:03:47

标签: android progressdialog

您好,我有一个Android应用程序,在我的应用程序webview中,我尝试制作ProgressDilog。当网站加载时,在活动标题中,每件事情都很有效,但在progressdialog中,我无法阻止它。这是我的代码:

mainWebView.setWebChromeClient(new WebChromeClient() {


           public void onProgressChanged(WebView view, int progress)
            {
                activity.setTitle("Loading...");
                activity.setProgress(progress * 100);
                 progressDialog = ProgressDialog.show(MainActivity.this, "", "Loading...");



                if(progress == 100)
                {   
                  activity.setTitle(mainWebView.getTitle());
                      progressDialog.dismiss();
                }   


            }


        });

但progressDialog.dismiss();不起作用:(

3 个答案:

答案 0 :(得分:4)

我认为你应该PD.show()只做一次,而不是每onProgressChanged()次。你可能有十几个PD正在运行。

编辑:更确切地说:当你开始做任何事情时,你都会pd.show()。在onProgressChanged()中,您使用setProgress()的某些内容(已加载的百分比或其他内容)更新您的进度条。

答案 1 :(得分:1)

请参阅这个简单的Demo,它将在每一步显示ProgressDialog。

public class AsyntaskActivity extends Activity {
 final Activity activity = this;
 private WebView webView; 
 private AlertDialog alert; 
 private Builder builder;
 protected void onCreate(Bundle savedInstanceState) {     
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.main);
     webView = (WebView)findViewById(R.id.tview);

     webView.setWebViewClient(new WebViewClient(){          
         @Override         
         public void onLoadResource(WebView view, String url) {             
             super.onLoadResource(view, url);         
         }          
         @Override         
         public void onPageFinished(WebView view, String url) {             
             super.onPageFinished(view, url);             
             alert.setTitle("Finish Loading. . ."); 
             alert.dismiss();
         }          
         @Override         
         public void onPageStarted(WebView view, String url, Bitmap favicon) {             
             super.onPageStarted(view, url, favicon);             
             alert.setTitle("Start Loading. . . ");         
        }      
    });     
     webView.setWebChromeClient(new WebChromeClient(){});     
     webView.loadUrl("http://www.google.com");     
     builder=new AlertDialog.Builder(this);     
     builder.setTitle("Loading...");     
     alert=builder.create();     
     alert.show(); 
} 

@Override protected void onDestroy() {     
    super.onDestroy();  
}  
@Override protected void onPause() {     
    super.onPause(); 
} 

}

答案 2 :(得分:0)

public void onProgressChanged(WebView view, int progress)
        {
            activity.setTitle("Loading...");
            activity.setProgress(progress + 100);
             progressDialog = ProgressDialog.show(MainActivity.this, "", "Loading...");



            if(progress == 100)
            {   
              activity.setTitle(mainWebView.getTitle());
                  progressDialog.dismiss();
            }   


        }


    });