Bootstrap 进度条未在 android 内递增
Bootstrap Progress bar not incrementing in android
我有以下代码,我想用它来缓慢增加我的进度条 20 秒
public void progressAnimator(){
final long period = 1000;
timer=new Timer();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
//this repeats every 100 ms
if (counter<100){
runOnUiThread(new Runnable() {
@Override
public void run() {
loaderLabel.setText(String.valueOf(counter)+"%");
}
});
mProgress.setProgress(counter);
counter++;
}
else{
//closing the timer
timer.cancel();
Intent intent =new Intent(SplashActivity.this,MainActivity.class);
startActivity(intent);
// close this activity
finish();
}
}
}, period);
}
我的问题是,这最终只会在 loaderLabel 上指示 0%,然后在不执行任何操作的情况下冻结。
我之前曾尝试过此代码,但它仅在 loaderLabel 上闪烁 100% 并填充进度条,然后继续进行到下一个 window.
public void progressAnimator(){
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
for( counter =1; counter<=100; counter ++) {
System.out.println(counter);
mProgress.setProgress(counter);
loaderLabel.setText(getResources().getString(R.string.loading) + " " + counter + " " + getResources().getString(R.string.percentSymbol));
if (counter == 100) {
Toast.makeText(SplashActivity.this, R.string.welcome, Toast.LENGTH_LONG).show();
Intent loadMain = new Intent(SplashActivity.this, MainActivity.class);
startActivity(loadMain);
finish();
}
}
}
}, 100);
}
如果我将延迟增加到 20,000 然后它冻结在零,我可能做错了什么?
public void progressAnimator(){
final long period = 1000;
timer=new Timer();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
//this repeats every 100 ms
if (counter<100){
runOnUiThread(new Runnable() {
@Override
public void run() {
loaderLabel.setText(String.valueOf(counter)+"%");
}
});
mProgress.setProgress(counter);
counter++;
} else{
//closing the timer
timer.cancel();
Intent intent =new Intent(SplashActivity.this,MainActivity.class);
startActivity(intent);
// close this activity
finish();
}
}
}, period);
}
在您的第一个代码片段中,您实例化了一个 Timer
对象,但您没有使用它来设置计时器。此外,在您的 Handler
实例中,没有循环可以确保 counter
不断增加。另外,如果有一个循环来增加 counter
,就像您之前的代码
public void progressAnimator(){
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
for( counter =1; counter<=100; counter ++) {
System.out.println(counter);
mProgress.setProgress(counter);
loaderLabel.setText(getResources().getString(R.string.loading) + " " + counter + " " + getResources().getString(R.string.percentSymbol));
if (counter == 100) {
Toast.makeText(SplashActivity.this, R.string.welcome, Toast.LENGTH_LONG).show();
Intent loadMain = new Intent(SplashActivity.this, MainActivity.class);
startActivity(loadMain);
finish();
}
}
}
}, 100);
}
您的循环的整个 100 次迭代是在瞬间执行的,因为没有 SLEEP
调用使循环实际等待 100 毫秒。您可以通过添加来解决这个问题
Thread.sleep(100)
在循环结束之前。但是,不建议这样做,因为它会导致整个 UI 线程被阻塞 100 毫秒。所以我建议你使用 CountDownTimer
来解决这个问题。
你可以这样使用它:
new CountDownTimer(total_duration, duration_of_one_step) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
// Write code to be executed on every iteration here
}
public void onFinish() {
mTextField.setText("done!");
// Write code to be executed on completing the timer here
}
}.start();
在你的例子中,total_duration
= 20000 和 duration_of_one_step
= 1000。
根据Kumar Harsh的回答,我建议你做以下事情
public void progressAnimator(){
new CountDownTimer(total_duration, duration_of_one_step) {
public void onTick(long millisUntilFinished) {
// code to be executed on every iteration
loaderLabel.setText(MessageFormat.format("{0} {1} {2}", getResources().getString(R.string.loading), ((total_duration - millisUntilFinished) / 1000) * 5, getResources().getString(R.string.percentSymbol)));
mProgress.setProgress((int) ((total_duration - millisUntilFinished) / 1000)*5);
}
public void onFinish() {
//code to be executed on completing the timer
Toast.makeText(SplashActivity.this, R.string.welcome, Toast.LENGTH_LONG).show();
Intent loadMain = new Intent(SplashActivity.this, MainActivity.class);
startActivity(loadMain);
finish();
}
}.start();
}
这应该可以解决您的问题,请注意,我乘以 5 使进度达到 100,否则它会停在 20
我有以下代码,我想用它来缓慢增加我的进度条 20 秒
public void progressAnimator(){
final long period = 1000;
timer=new Timer();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
//this repeats every 100 ms
if (counter<100){
runOnUiThread(new Runnable() {
@Override
public void run() {
loaderLabel.setText(String.valueOf(counter)+"%");
}
});
mProgress.setProgress(counter);
counter++;
}
else{
//closing the timer
timer.cancel();
Intent intent =new Intent(SplashActivity.this,MainActivity.class);
startActivity(intent);
// close this activity
finish();
}
}
}, period);
}
我的问题是,这最终只会在 loaderLabel 上指示 0%,然后在不执行任何操作的情况下冻结。 我之前曾尝试过此代码,但它仅在 loaderLabel 上闪烁 100% 并填充进度条,然后继续进行到下一个 window.
public void progressAnimator(){
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
for( counter =1; counter<=100; counter ++) {
System.out.println(counter);
mProgress.setProgress(counter);
loaderLabel.setText(getResources().getString(R.string.loading) + " " + counter + " " + getResources().getString(R.string.percentSymbol));
if (counter == 100) {
Toast.makeText(SplashActivity.this, R.string.welcome, Toast.LENGTH_LONG).show();
Intent loadMain = new Intent(SplashActivity.this, MainActivity.class);
startActivity(loadMain);
finish();
}
}
}
}, 100);
}
如果我将延迟增加到 20,000 然后它冻结在零,我可能做错了什么?
public void progressAnimator(){
final long period = 1000;
timer=new Timer();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
//this repeats every 100 ms
if (counter<100){
runOnUiThread(new Runnable() {
@Override
public void run() {
loaderLabel.setText(String.valueOf(counter)+"%");
}
});
mProgress.setProgress(counter);
counter++;
} else{
//closing the timer
timer.cancel();
Intent intent =new Intent(SplashActivity.this,MainActivity.class);
startActivity(intent);
// close this activity
finish();
}
}
}, period);
}
在您的第一个代码片段中,您实例化了一个 Timer
对象,但您没有使用它来设置计时器。此外,在您的 Handler
实例中,没有循环可以确保 counter
不断增加。另外,如果有一个循环来增加 counter
,就像您之前的代码
public void progressAnimator(){
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
for( counter =1; counter<=100; counter ++) {
System.out.println(counter);
mProgress.setProgress(counter);
loaderLabel.setText(getResources().getString(R.string.loading) + " " + counter + " " + getResources().getString(R.string.percentSymbol));
if (counter == 100) {
Toast.makeText(SplashActivity.this, R.string.welcome, Toast.LENGTH_LONG).show();
Intent loadMain = new Intent(SplashActivity.this, MainActivity.class);
startActivity(loadMain);
finish();
}
}
}
}, 100);
}
您的循环的整个 100 次迭代是在瞬间执行的,因为没有 SLEEP
调用使循环实际等待 100 毫秒。您可以通过添加来解决这个问题
Thread.sleep(100)
在循环结束之前。但是,不建议这样做,因为它会导致整个 UI 线程被阻塞 100 毫秒。所以我建议你使用 CountDownTimer
来解决这个问题。
你可以这样使用它:
new CountDownTimer(total_duration, duration_of_one_step) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
// Write code to be executed on every iteration here
}
public void onFinish() {
mTextField.setText("done!");
// Write code to be executed on completing the timer here
}
}.start();
在你的例子中,total_duration
= 20000 和 duration_of_one_step
= 1000。
根据Kumar Harsh的回答,我建议你做以下事情
public void progressAnimator(){
new CountDownTimer(total_duration, duration_of_one_step) {
public void onTick(long millisUntilFinished) {
// code to be executed on every iteration
loaderLabel.setText(MessageFormat.format("{0} {1} {2}", getResources().getString(R.string.loading), ((total_duration - millisUntilFinished) / 1000) * 5, getResources().getString(R.string.percentSymbol)));
mProgress.setProgress((int) ((total_duration - millisUntilFinished) / 1000)*5);
}
public void onFinish() {
//code to be executed on completing the timer
Toast.makeText(SplashActivity.this, R.string.welcome, Toast.LENGTH_LONG).show();
Intent loadMain = new Intent(SplashActivity.this, MainActivity.class);
startActivity(loadMain);
finish();
}
}.start();
}
这应该可以解决您的问题,请注意,我乘以 5 使进度达到 100,否则它会停在 20