gpt4 book ai didi

安卓进度条 : loading with Timer

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:03:33 25 4
gpt4 key购买 nike

Update: This post was from when I was learning android with android 2.2, be sure to check if this is compatible with the api level you are working with.

四处寻找如何使用计时器加载进度条,我尝试使用已发布的方法,但总是会出现指针异常,或者测试应用程序会在启动时崩溃。

我的问题是,大声笑有没有人看过任何关于如何做到这一点的教程或示例?我在想我所需要的只是一个 while 循环,但还没有看到在计时器上完成的。我是一个完全的菜鸟,但正在缓慢但肯定地学习

我的计时器

final Timer t = new Timer();
t.schedule(new TimerTask() {
public void run() {
d.dismiss(); //MY DIALOG WINDOW
t.cancel();
}
}, 7000);

这是我试图开始工作但认为我造成的伤害比用它做得更好

isRunning = false;
// handler for the background updating
final Handler handler = new Handler() {
public void handleMessage(Message msg) {
total = total - 1;
String perc = String.valueOf(total).toString();
a.setText(perc + " SECONDS until close");
bar.incrementProgressBy(25);
}
};

super.onStart();
// reset the bar to the default value of 0
bar.setProgress(0);
// create a thread for updating the progress bar
Thread background = new Thread(new Runnable() {
public void run() {
try {
for (int i = 4; i < 20 && isRunning; i++) {
// wait 1000ms between each update
Thread.sleep(1000);
handler.sendMessage(handler.obtainMessage());
}
} catch (Throwable t) {
}
}
});
isRunning = true;
// start the background thread
background.start();

希望有人能够为我阐明这一点,一如既往地对简短的通用问题感到抱歉,只是想了解如何使用计时器加载进度条

再次感谢所有帮助

最佳答案

使用android.os.CountDownTimer

 countDownTimer = new CountDownTimer(length_in_milliseconds,period_in_milliseconds) {
private boolean warned = false;
@Override
public void onTick(long millisUntilFinished_) {
progressBar.progress = (length_in_milliseconds-millisUntilFinished_)/lenght_in_milliseconds*100.0;
}

@Override
public void onFinish() {
// do whatever when the bar is full
}
}.start();

关于安卓进度条 : loading with Timer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8397212/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com