代码之家  ›  专栏  ›  技术社区  ›  impossible

Android中任务集的重复ProgressBar活动

  •  0
  • impossible  · 技术社区  · 9 年前

    我对android是新手,没有多少管理线程的经验。我正在进行一项活动,我想在其中显示进度条,比如5秒钟,然后重复。在这5秒钟内,我将显示一些文本,供用户处理文本。我想重复N次。

    目前,我有以下代码,为1这样的进展工作。我尝试循环它,但并没有帮助,因为线程同时执行。如何重复N次?为了解决我的问题,我走的是正确的道路吗?

    public class test extends Activity {
    
        private ProgressBar progressBar;
        private int progressStatus = 0;
        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
    
            for(int i=0; i<5; i++)
                Progress();
    
        }
    
        public void Progress(){
            new Thread(new Runnable() {
                public void run() {
                    while (progressStatus < 100) {
                        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();
        }
    }
    
    1 回复  |  直到 9 年前
        1
  •  0
  •   Dr. Nitpick    9 年前

    我不确定我是否建议使用您所遵循的模式,但以下是让线程一个接一个地运行而不是同时运行所有线程的方法:

    public void progress(final int numberOfRuns) {
        if (numberOfRuns <= 0 ) {
            return;
        }
        new Thread(new Runnable() {
            public void run() {
                while (progressStatus < 100) {
                    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());
                            // For the UI Changes. Eg update the loop number
                            myTextView.setText(Integer.toString(totalLoop));
                        }
                    });
                    try {
                        // Sleep for 200 milliseconds.
                        //Just to display the progress slowly
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                progressStatus = 0;
                totalLoop = totalLoop+1;
                progress(numberOfRuns - 1);
            }
        }).start();
    }
    

    那就打电话 progress(numberOfRuns) ,不需要任何循环。