2016-07-05 63 views
0

通過靜態我的意思是,我有一些數據說20,其最大值可以是100.我想命名它的進展。然後我需要一個活動,當我們打開進度選項卡時會被調用。它加載進度並在達到20時停止。在Android中創建靜態進度條

它最終應該看起來像。

Progress. 
++++---------------- 
Progress1. 
+++++++++++--------- 
Progress3. 
+++++++------------- 

我用Google搜索,並得到這個GIF這正是告訴我的要求。 GIF

我嘗試和可以實現的是:

活動

public class test extends Activity { 
    private ProgressBar progressBar; 
    private int progressStatus = 0, CurrValue=20; 
    private TextView textView; 
    private Handler handler = new Handler(); 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_loop); 
     progressBar = (ProgressBar) findViewById(R.id.progressBar1); 
     textView = (TextView) findViewById(R.id.textView1); 
     progressBar.setScaleY(3f); 
     // Start long running operation in a background thread 
     Progress(); 
    } 
    public void Progress(){ 
     new Thread(new Runnable() { 
      public void run() { 
       while (progressStatus < CurrValue) { 
        progressStatus += 1; 
        // Update the progress bar and display the 
        //current value in the text view 
        handler.post(new Runnable() { 
         public void run() { 
          progressBar.setProgress(progressStatus); 
          textView.setText(progressStatus+"/"+progressBar.getMax()); 
         } 
        }); 
        try { 
         // Sleep for 200 milliseconds. 
         //Just to display the progress slowly 
         Thread.sleep(100); 
        } catch (InterruptedException e) { 
         e.printStackTrace(); 
        } 
       } 
      } 
     }).start(); 
    } 
} 

佈局

它有一個TextView和進度

我想說明多個進度條,動畫只是裏在上面的GIF鏈接中。

回答

1

這裏有一個想法:

  1. 創建ProgressUpdater類。它在構造函數中使用ProgressBar,監視進度的變量以及最大值(假設0是最小值或最小值,如果它不是0到最大值)。
  2. 它有一個UpdateProgress()方法,您可以調用它來檢查變量並計算進度/更新ProgressBar。
  3. 您的活動包含一個ProgressUpdater數組,它在每個ProgressBar的onCreate中初始化。
  4. Progress()然後在ProgressUpdaters數組中循環並在每一個上調用UpdateProgress()。您需要對Progress()進行一些更改以處理多個值,或者改爲使用一些Listener並讓它調用相應ProgressUpdater的UpdateProgress()方法。

這聽起來怎麼樣?

+0

我做到了。它的工作。 :D謝謝! – impossible

+0

@ArewegoodQ真棒,很棒的工作! – iheanyi