【问题标题】:Synchronizing the Progress Bar with the Timer将进度条与计时器同步
【发布时间】:2010-12-06 07:18:06
【问题描述】:

我想在单击按钮时同时更新我的​​ ProgressBar 和音频文件。我还使用了根据音频文件长度(以秒为单位)运行的计时器。

我已经配置了计时器,它会根据音频文件的长度很好地增加,但我的 ProgressBar 不工作。它给了我 NumberFormatException。

我的代码: MyCount 类扩展了 CountDownTimer { 私人 int mycount;

        public MyCount(long millisInFuture, long countDownInterval) 
        {
          super(millisInFuture, countDownInterval);
        }

        public void onFinish() 
        {
          //timeDisplay.setText("done!");
        }
        public void onTick(long millsIncreasing) 
        {

        mycount++;
        mystring = formatTime(mycount*1000);
        myText.setText(mystring); 
        pBar.setProgress(Integer.parseInt(mystring));  (*****)
        pBar.setProgress(1000 * pBar.getProgress());
        /*String myString1 = mystring.substring(0, mystring.lastIndexOf(":")).trim();
        String myString2 = mystring.substring(mystring.lastIndexOf(":")+1, mystring.length()).trim();*/

我在括号中显示星星的那一行出现异常。 任何人都可以帮助我, 谢谢 大卫

【问题讨论】:

  • 你能把formatTime函数的代码贴出来吗?它会返回一个格式化的整数吗?
  • 当你得到异常时你能调试它和/或记录 mystring 是什么吗?
  • 为什么要将数字“mycount”转换为时间字符串,然后尝试将其转换回整数?
  • @Scrum 大师,这是我的 FormatTime 函数的代码:
  • @Scrum Meister, public String formatTime(long millis) { String output = "00:00";长秒 = 毫秒 / 1000;长分钟 = 秒 / 60;秒 = 秒 % 60;分钟 = 分钟 % 60; String secondsD = String.valueOf(seconds);字符串分钟D = String.valueOf(分钟); if (seconds

标签: android timer progress-bar


【解决方案1】:

由于您的函数返回的字符串“12:34”(中间有 :)不是 Integer.parse 可以解析的数字格式。

你应该保留一个变量来保存文件时间长度的值(以秒为单位)

int fileLength

然后在你的函数中:

public void onTick(long millsIncreasing) 
{
    int progress = (int)Math.round(millsIncreasing / fileLength) * 100;
    Bar.setProgress(progress);

这是假设millsIncreasing 参数是毫秒。处理到此为止。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多