2011-06-23 31 views
3

我希望有人能幫助我弄清楚如何改變setmessage對話框中進度只是一個僞定時器對話框,通過任何一個字符串數組或任何其他週期辦法。例如,當它加載它可以說加載... - >建築... - >渲染... - >等等。像1/2秒計時器(這只是我用它玩我能適應它使用實時更新,一旦我弄清楚如何改變對話框。)我一直在使用this tutorial時進度線程減去方向碼。有任何想法嗎?安卓進度對話框更改ProgressDialog.setMessage(),同時加載

謝謝!

它不漂亮,但我能得到它與這個工作:

public class BadBaconJoke extends Activity implements OnClickListener { 
    ProgressDialog dialog; 
    int increment; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.progressdialog); 

     Button startbtn = (Button) findViewById(R.id.startbtn); 
     startbtn.setOnClickListener(this); 
    } 

    public void onClick(View view) { 

     // get the increment value from the text box 
     EditText et = (EditText) findViewById(R.id.increment); 
     // convert the text value to a integer 
     increment = Integer.parseInt(et.getText().toString()); 
     dialog = new ProgressDialog(this); 
     dialog.setCancelable(true); 
     dialog.setTitle("Loading..."); 
     dialog.setMessage("Almsot done!"); 
     // set the progress to be horizontal 
     dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 

     // reset the bar to the default value of 0 
     dialog.setProgress(0); 

     // get the maximum value 
     EditText max = (EditText) findViewById(R.id.maximum); 
     // convert the text value to a integer 
     final int maximum = Integer.parseInt(max.getText().toString()); 
     // set the maximum value 
     dialog.setMax(maximum); 
     // display the progressbar 
     dialog.show(); 


     // create a thread for updating the progress bar 
     Thread background = new Thread (new Runnable() { 
      public void run() { 
       try { 


        // enter the code to be run while displaying the progressbar. 
        // 
        // This example is just going to increment the progress bar: 
        // So keep running until the progress value reaches maximum value 
        while (dialog.getProgress()<= dialog.getMax()) { 
         // wait 500ms between each update 
         Thread.sleep(500); 

         // active the update handler 
         progressHandler.sendMessage(progressHandler.obtainMessage()); 


        } 
       } catch (java.lang.InterruptedException e) { 
        // if something fails do something smart 
       } 
      } 
     }); 

     // start the background thread 
     background.start(); 



    } 

    // handler for the background updating 
    Handler progressHandler = new Handler() { 
     public void handleMessage(Message msg) { 
      dialog.incrementProgressBy(increment); 
      int x; 
      x = dialog.getProgress(); 

      switch (x) { 
      case 10: dialog.setMessage("Gathering Data");  break; 
      case 20: dialog.setMessage("Parsing Results");  break; 
      case 30: dialog.setMessage("Builindg Data");  break; 
      case 40: dialog.setMessage("TeraForming");  break; 
      case 50: dialog.setMessage("Contacting Server"); break; 
      case 60: dialog.setMessage("Ping Back"); break; 
      case 70: dialog.setMessage("Processing"); break; 
      case 80: dialog.setMessage("Communicating with Device"); break; 
      case 90: dialog.setMessage("Populating"); break; 
      case 100: dialog.setMessage("Displaying Data:"); break; 
      default: dialog.setMessage("..."); break; 
     } 

     if (x == 100){ 
      new AlertDialog.Builder(BadBaconJoke.this); 
      dialog.setTitle("Complete"); 
      //.setMessage("Are you sure you want to exit?") 
      // dialog.setButton(android.R.string.no, null); 
      //.setMessage("Are you sure you want to exit?") 
     } 


     } 
    }; 










} 

任何想法如何得到它使用旋轉器VS進程對話框工作?當我改變dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);到dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);

它僅顯示從開關殼體的缺省值。

回答

3

您可以使用onProgressUpdated()爲了更改消息,因爲這種方法是在UI線程上運行。儘管如此,它並沒有給你機會在特定的時間內改變信息。