CountDownTimer - ProgressBar停止在90%(Java)

问题描述:

我想在10秒内点击按钮后填写ProgressBar。问题在于ProgressBar的进度停止在90%左右。CountDownTimer - ProgressBar停止在90%(Java)

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

+0

什么是我的最后一个值? – immibis

+0

i的最后一个值是i = 89 – Mapi

+0

我的猜测是你的一些跳动被跳过了,因为系统忙于做100毫秒的其他动作。您应该根据'millisUntilFinished'的值设置进度条。 – immibis

这一点,因为你错过了两次单击这里是代码我生成解决您的问题,我尝试了很多数字与我,但我想我需要的只是3日开始的第一跳时,我= 4时间,直到完成将是9797(有时候这次改为9860),但这是平均值,所以我们发现我们错过了两次滴答。你需要在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

Tick of Progress; i:4// 9797

第二部分代码

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 

我将承担最后一跳不会到来,因为计时器之前完成。为什么不在最后一步设置onFinish的进度条(“只是为了确定”)?