CountDownTimer - ProgressBar停止在90%(Java)

时间:2016-08-02 09:19:36

标签: android countdowntimer

我希望在10秒内点击按钮后填充ProgressBar。问题是ProgressBar的进度停止在90%左右。

public void klick(View v){

    mProgressBar=(ProgressBar)findViewById(R.id.progressbar);
    mProgressBar.setProgress(i);
    mProgressBar.setMax(100);
    mCountDownTimer=new CountDownTimer(10000,100) {

        @Override
        public void onTick(long millisUntilFinished) {
            Log.v("Log_tag", "Tick of Progress; i:"+ i+ "// "+ millisUntilFinished);
            i++;
            mProgressBar.setProgress(i);

        }

        @Override
        public void onFinish() {
            //Do what you want
            b1.setText("Finished");
        }
    };
    mCountDownTimer.start();
}

为什么停止?我认为我的代码每0.1秒运行onTick() 10秒,所以{10}内i = 100。{/ p>

2 个答案:

答案 0 :(得分:0)

我会假设最后一次打勾不会到达,因为计时器在之前完成。为什么不在最后一步onFinish中设置进度条("只是为了确定")?

答案 1 :(得分:0)

这是因为你错过了两次点击这里是我生成的代码来修复你的问题,我用i尝试了很多数字,但我认为我需要以3开始作为第一个勾选当i = 4时间直到完成将是9797(有时候这个时间改为9860)但这是平均值所以我们发现我们错过了两个滴答声。

Tick of Progress; i:4// 9797

第二部分您需要在onFinish更新进度,以避免多丢一个标记

<强> UPDATE_ANSWER

             @Override
            public void onTick(long millisUntilFinished) {
                 // 100 is the max value       
                i = 100 - (int) millisUntilFinished / 100 ;
                mProgressBar.setProgress(i);

      Log.v("Log_tag", "Tick of Progress; i:" + i + "// " + millisUntilFinished);

            }

            @Override
            public void onFinish() {
                //you will figure you lost tick
                // so I handle it with i++  
                Log.v("Log_tag", "Last tick : " +i);
                mProgressBar.setProgress(i++);
                mtextview.setText("Finished");
            }

这是java代码和日志

Java代码

private ProgressBar mProgressBar;
private CountDownTimer mCountDownTimer;
int i = 3;
TextView mtextview;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mtextview = (TextView) findViewById(R.id.textview);
    klick();  // handle tick as you wish 
}

public void klick() {

    mProgressBar = (ProgressBar) findViewById(R.id.progressbar);
    mProgressBar.setProgress(i);
    mProgressBar.setMax(100);
    mCountDownTimer = new CountDownTimer(10000, 100) {

        @Override
        public void onTick(long millisUntilFinished) {
            Log.v("Log_tag", "Tick of Progress; i:" + i + "// " + millisUntilFinished);
            i++;
            mProgressBar.setProgress(i);

        }

        @Override
        public void onFinish() {
            //Do what you want
            Log.v("Log_tag", "Last tick : " +i);
            mProgressBar.setProgress(i);
            mtextview.setText("Finished");
        }
    };
    mCountDownTimer.start();
}

记录

08-02 06:26:09.637 Log_tag: Tick of Progress; i:4// 9797
08-02 06:26:09.738 Log_tag: Tick of Progress; i:5// 9696
08-02 06:26:09.839 Log_tag: Tick of Progress; i:6// 9596
08-02 06:26:09.939 Log_tag: Tick of Progress; i:7// 9495
08-02 06:26:10.046 Log_tag: Tick of Progress; i:8// 9388
08-02 06:26:10.162 Log_tag: Tick of Progress; i:9// 9272
08-02 06:26:10.263 Log_tag: Tick of Progress; i:10// 9171
08-02 06:26:10.364 Log_tag: Tick of Progress; i:11// 9070
08-02 06:26:10.464 Log_tag: Tick of Progress; i:12// 8970
08-02 06:26:10.564 Log_tag: Tick of Progress; i:13// 8870
08-02 06:26:10.665 Log_tag: Tick of Progress; i:14// 8769
08-02 06:26:10.765 Log_tag: Tick of Progress; i:15// 8669
08-02 06:26:10.866 Log_tag: Tick of Progress; i:16// 8568
08-02 06:26:10.967 Log_tag: Tick of Progress; i:17// 8467
08-02 06:26:11.069 Log_tag: Tick of Progress; i:18// 8365
08-02 06:26:11.170 Log_tag: Tick of Progress; i:19// 8264
08-02 06:26:11.272 Log_tag: Tick of Progress; i:20// 8163
08-02 06:26:11.371 Log_tag: Tick of Progress; i:21// 8063
08-02 06:26:11.472 Log_tag: Tick of Progress; i:22// 7963
08-02 06:26:11.574 Log_tag: Tick of Progress; i:23// 7861
08-02 06:26:11.673 Log_tag: Tick of Progress; i:24// 7761
08-02 06:26:11.774 Log_tag: Tick of Progress; i:25// 7660
08-02 06:26:11.874 Log_tag: Tick of Progress; i:26// 7560
08-02 06:26:11.975 Log_tag: Tick of Progress; i:27// 7459
08-02 06:26:12.077 Log_tag: Tick of Progress; i:28// 7357
08-02 06:26:12.178 Log_tag: Tick of Progress; i:29// 7256
08-02 06:26:12.294 Log_tag: Tick of Progress; i:30// 7141
08-02 06:26:12.394 Log_tag: Tick of Progress; i:31// 7040
08-02 06:26:12.496 Log_tag: Tick of Progress; i:32// 6938
08-02 06:26:12.597 Log_tag: Tick of Progress; i:33// 6837
08-02 06:26:12.699 Log_tag: Tick of Progress; i:34// 6736
08-02 06:26:12.799 Log_tag: Tick of Progress; i:35// 6636
08-02 06:26:12.899 Log_tag: Tick of Progress; i:36// 6535
08-02 06:26:13.000 Log_tag: Tick of Progress; i:37// 6434
08-02 06:26:13.101 Log_tag: Tick of Progress; i:38// 6334
08-02 06:26:13.201 Log_tag: Tick of Progress; i:39// 6233
08-02 06:26:13.302 Log_tag: Tick of Progress; i:40// 6132
08-02 06:26:13.403 Log_tag: Tick of Progress; i:41// 6031
08-02 06:26:13.505 Log_tag: Tick of Progress; i:42// 5929
08-02 06:26:13.605 Log_tag: Tick of Progress; i:43// 5829
08-02 06:26:13.706 Log_tag: Tick of Progress; i:44// 5728
08-02 06:26:13.811 Log_tag: Tick of Progress; i:45// 5624
08-02 06:26:13.912 Log_tag: Tick of Progress; i:46// 5522
08-02 06:26:14.012 Log_tag: Tick of Progress; i:47// 5422
08-02 06:26:14.112 Log_tag: Tick of Progress; i:48// 5322
08-02 06:26:14.213 Log_tag: Tick of Progress; i:49// 5221
08-02 06:26:14.315 Log_tag: Tick of Progress; i:50// 5120
08-02 06:26:14.415 Log_tag: Tick of Progress; i:51// 5019
08-02 06:26:14.518 Log_tag: Tick of Progress; i:52// 4916
08-02 06:26:14.619 Log_tag: Tick of Progress; i:53// 4815
08-02 06:26:14.721 Log_tag: Tick of Progress; i:54// 4713
08-02 06:26:14.823 Log_tag: Tick of Progress; i:55// 4611
08-02 06:26:14.924 Log_tag: Tick of Progress; i:56// 4511
08-02 06:26:15.023 Log_tag: Tick of Progress; i:57// 4411
08-02 06:26:15.124 Log_tag: Tick of Progress; i:58// 4311
08-02 06:26:15.227 Log_tag: Tick of Progress; i:59// 4207
08-02 06:26:15.327 Log_tag: Tick of Progress; i:60// 4107
08-02 06:26:15.434 Log_tag: Tick of Progress; i:61// 4001
08-02 06:26:15.541 Log_tag: Tick of Progress; i:62// 3900
08-02 06:26:15.634 Log_tag: Tick of Progress; i:63// 3800
08-02 06:26:15.735 Log_tag: Tick of Progress; i:64// 3699
08-02 06:26:15.835 Log_tag: Tick of Progress; i:65// 3599
08-02 06:26:15.935 Log_tag: Tick of Progress; i:66// 3499
08-02 06:26:16.037 Log_tag: Tick of Progress; i:67// 3398
08-02 06:26:16.137 Log_tag: Tick of Progress; i:68// 3297
08-02 06:26:16.238 Log_tag: Tick of Progress; i:69// 3196
08-02 06:26:16.340 Log_tag: Tick of Progress; i:70// 3095
08-02 06:26:16.439 Log_tag: Tick of Progress; i:71// 2995
08-02 06:26:16.540 Log_tag: Tick of Progress; i:72// 2894
08-02 06:26:16.641 Log_tag: Tick of Progress; i:73// 2793
08-02 06:26:16.741 Log_tag: Tick of Progress; i:74// 2693
08-02 06:26:16.843 Log_tag: Tick of Progress; i:75// 2591
08-02 06:26:16.943 Log_tag: Tick of Progress; i:76// 2491
08-02 06:26:17.043 Log_tag: Tick of Progress; i:77// 2391
08-02 06:26:17.143 Log_tag: Tick of Progress; i:78// 2291
08-02 06:26:17.244 Log_tag: Tick of Progress; i:79// 2190
08-02 06:26:17.345 Log_tag: Tick of Progress; i:80// 2090
08-02 06:26:17.444 Log_tag: Tick of Progress; i:81// 1990
08-02 06:26:17.545 Log_tag: Tick of Progress; i:82// 1889
08-02 06:26:17.648 Log_tag: Tick of Progress; i:83// 1786
08-02 06:26:17.749 Log_tag: Tick of Progress; i:84// 1685
08-02 06:26:17.850 Log_tag: Tick of Progress; i:85// 1585
08-02 06:26:17.951 Log_tag: Tick of Progress; i:86// 1483
08-02 06:26:18.056 Log_tag: Tick of Progress; i:87// 1378
08-02 06:26:18.157 Log_tag: Tick of Progress; i:88// 1277
08-02 06:26:18.258 Log_tag: Tick of Progress; i:89// 1177
08-02 06:26:18.358 Log_tag: Tick of Progress; i:90// 1076
08-02 06:26:18.461 Log_tag: Tick of Progress; i:91// 973
08-02 06:26:18.562 Log_tag: Tick of Progress; i:92// 872
08-02 06:26:18.662 Log_tag: Tick of Progress; i:93// 772
08-02 06:26:18.764 Log_tag: Tick of Progress; i:94// 670
08-02 06:26:18.865 Log_tag: Tick of Progress; i:95// 569
08-02 06:26:18.965 Log_tag: Tick of Progress; i:96// 469
08-02 06:26:19.066 Log_tag: Tick of Progress; i:97// 368
08-02 06:26:19.167 Log_tag: Tick of Progress; i:98// 267
08-02 06:26:19.270 Log_tag: Tick of Progress; i:99// 164
08-02 06:26:19.435 Log_tag: Last tick : 100