Seekbar与计时器同步

时间:2014-05-18 21:07:04

标签: java android

我是Android的编程新手,我正在试图弄清楚如何将搜索栏与我的计时器相关联。在我的程序中,我允许用户输入一个时间,然后单击启动计时器,然后将该值发送到TextView并递减秒直到完成。我希望搜索栏能够“跟随”。任何帮助,将不胜感激!

我的搜索栏有点跟随计时器,但现在它太过波涛汹涌......是否有一种简单的方法可以顺利进行?

public void startTimer() {
    int min = Integer.parseInt(edit_minute.getText().toString());
    int sec = Integer.parseInt(edit_second.getText().toString());
    int value = (min * 60) + sec;
    seek_bar.setMax(value *10000);

    new CountDownTimer(getValues(), 1000) { // adjust the milli seconds here

        public void onTick(long millisUntilFinished) {


            text1.setText(""
                    + String.format(
                            FORMAT,
                            TimeUnit.MILLISECONDS
                                    .toHours(millisUntilFinished),
                            TimeUnit.MILLISECONDS
                                    .toMinutes(millisUntilFinished)
                                    - TimeUnit.HOURS
                                            .toMinutes(TimeUnit.MILLISECONDS
                                                    .toHours(millisUntilFinished)),
                            TimeUnit.MILLISECONDS
                                    .toSeconds(millisUntilFinished)
                                    - TimeUnit.MINUTES
                                            .toSeconds(TimeUnit.MILLISECONDS
                                                    .toMinutes(millisUntilFinished))));
            seek_bar.setProgress((seek_bar.getProgress() + 1));
        }

        public void onFinish() {
            text1.setText("done!");
        }
    }.start();

1 个答案:

答案 0 :(得分:1)

您只需要从CountDownTimer更新它。首先,创建一个实例变量:

SeekBar progressBar;

onCreate内,获取对搜索栏的引用:

progressBar = (SeekBar) findViewById(R.id.seek_bar_current);

启动计时器后,告诉progressBar它应该达到的最大值:

progressBar.setMax(); // How ever long the timer will run for

然后在CountDownTimer中,更新进度:

progressBar.setProgress(); // The total time, minus millisUntilFinished