【安卓笔记】Handler:10秒钟时间显示进度条
方式一:使用Handler
public class ProgressActivity extends Activity implements Runnable {
private ProgressBar progress;
Handler h = new Handler();
private int max=100,current,step;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_progress);
progress = (ProgressBar) findViewById(R.id.progress);
progress.setMax(max);
progress.setProgress(0);
step = max/10;
h.post(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
}
@Override
public void run() {
current = progress.getProgress();
progress.setProgress(current+step);
h.postDelayed(this, 1000);
}
}
方式二:使用自定义线程
有些微不对劲
public class ProgressActivity extends Activity {
private ProgressBar progress;
private int max = 100, current = 0, step = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_progress);
progress = (ProgressBar) findViewById(R.id.progress);
progress.setMax(max);
progress.setProgress(0);
step = max / 10;
new Thread(new Runnable() {
int i = 1;
@Override
public void run() {
try {
while (max != progress.getProgress()) {
Log.i("次数", i + "");
i++;
progress.setProgress(current + step);
current = progress.getProgress();
Thread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
@Override
protected void onDestroy() {
super.onDestroy();
}
}
相关推荐
xfcyhades 2020-11-20
Michael 2020-11-03
业余架构师 2020-10-09
OuNuo0 2020-09-29
moses 2020-09-22
Angelia 2020-09-11
qinxu 2020-09-10
刘炳昭 2020-09-10
Nostalgiachild 2020-09-07
Nostalgiachild 2020-08-17
leavesC 2020-08-14
一青年 2020-08-13
AndroidAiStudy 2020-08-07
ydc0 2020-07-30
绿豆饼 2020-07-28