如何显示1分钟的进度条? [英] How to display a progress bar for 1 minute?

查看:19
本文介绍了如何显示1分钟的进度条?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Android中有一个水平进度条。我需要在60秒内完成。

为什么以下代码不起作用?

int progress = 0;
progressBarHorizontal.setMax(60);
while(progress < 60) {
     progressHandler.postDelayed((new Runnable() {
        public void run() {                     
           progressBarHorizontal.setProgress(progress);                    
        }                   
     }),progress * 1000);
     progress++;
}

请推荐一些其他方法。我试过了:

new Thread(new Runnable() {
                int progress = 0;       
                public void run() {
                    long timerEnd = System.currentTimeMillis() + 60 * 1000;

                    while (timerEnd >  System.currentTimeMillis() ) {

                        progress = (int) (timerEnd - System.currentTimeMillis()) / 1000;
                        // Update the progress bar
                        progressHandler.post(new Runnable() {
                            public void run() {
                                progressBarHorizontal.setProgress(progress);
                            }
                        });                         
                        try {
                            Thread.sleep(500);
                        } catch (InterruptedException e) {
                            Log.w("tag","Progress thread cannot sleep");
                        }
                    }
                }
            }).start(); 

但它也不起作用。

第二段代码实际起作用了,问题出在我的逻辑上。

推荐答案

此代码适合我

new Thread(new Runnable() {

                public void run() {
                    long timerEnd = System.currentTimeMillis() + 60 * 1000;

                    while (timerEnd >  System.currentTimeMillis()) {

                        progress = 60 - (int) (timerEnd - System.currentTimeMillis()) / 1000;
                        // Update the progress bar

                        progressHandler.post(new Runnable() {
                            public void run() {                     
                                    progressBarHorizontal.setProgress(progress);                    
                            }                   
                        });

                        try {
                            Thread.sleep(500);
                        } catch (InterruptedException e) {
                            Log.w("App","Progress thread cannot sleep");
                        }
                    }
                    progressHandler.post(new Runnable() {
                        public void run() {                     
                                okButton.performClick();                    
                        }                   
                    });
                }
            }).start(); 

这篇关于如何显示1分钟的进度条?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆