2016-07-05 92 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