2012-03-09 79 views
1

我在我的android應用程序中創建了一個ProgressDialog。但是我遇到的問題是,在實際上停止旋轉車輪的過程中。這是我的代碼。我如何做到這一點,以便在我的其他工作正在進行時不斷旋轉車輪?Android ProgressDialog停止旋轉

button5.setOnClickListener(new View.OnClickListener() 
{ 
public void onClick(View v) 
{ 
    System.out.println("Button5"); 

    //Handler to make the please wait message 
    final ProgressDialog myProgressDialog = ProgressDialog.show(
      FoodSubstitutesActivity.this, "Please wait...", 
      "Getting most recent updates...", true); 
    Handler handler = new Handler(); 
    handler.postDelayed(new Runnable() 
    { 
     @Override 
     public void run() 
     { 
      //DO STUFF - STOPS SPINNING WHEEL UNTIL THIS PART IS COMPLETE. 
      myProgressDialog.dismiss(); 
     } 

    }, 500); 
} 
}); 
+0

您阻塞UI線程 – slezica 2012-03-09 04:32:57

回答

1

爲什麼不試試這樣做呢?

final ProgressDialog dialog = ProgressDialog.show(this, "Title", 
"Message", true); 
final Handler handler = new Handler() { 
    public void handleMessage(Message msg) { 
     dialog.dismiss(); 
     } 
    }; 
Thread checkUpdate = new Thread() { 
    public void run() { 
// 
// YOUR LONG CALCULATION (OR OTHER) GOES HERE 
// 
     handler.sendEmptyMessage(0); 
     } 
    }; 
checkUpdate.start(); 

摘自:http://www.tutorials-android.com/learn/How_to_display_a_progress_dialog_while_computing.rhtml

+0

這真是棒極了!謝謝!爲什麼最後你必須發送一個空的消息? – 2012-03-09 04:33:41

0

使用此代碼,這可能幫助你,

   // TODO Auto-generated method stub 
       myProgressDialog = ProgressDialog.show(MainActivity.this, 
         "", "Please wait...."); 
       myProgressDialog 
         .setProgressStyle(ProgressDialog.STYLE_SPINNER); 

       new Thread() { 
        public void run() { 
         try { 

          Thread.sleep(1000); 
         } catch (Exception e) { 
         } 

         runOnUiThread(new Runnable() { 
          @Override 
          public void run() { 
           // do your action........... 
           finish(); 

          } 
         }); 
         // Dismiss the Dialog 
         myProgressDialog.dismiss(); 
        } 
       }.start(); 

謝謝...