【发布时间】:2016-11-06 11:46:46
【问题描述】:
我是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();
}
}
【问题讨论】:
-
During those 5 seconds, I will display some text for the user to work on the text.???处理 TextView 中显示的文本?用户应该如何处理它? -
不要同时启动五个线程。一个线程完成后启动下一个线程。或者让run()中的代码循环运行五次。
-
用户看到五次进度有什么意义?
-
我的意思是,我会测试用户的记忆力。我会将文本保留 5 秒钟。最后我会以某种方式对其进行测试。
标签: android multithreading android-progressbar