验证码倒计时

public void onCodeClick() {
    final long count = 60; // 设置60秒    
    Observable.interval(0, 1, TimeUnit.SECONDS)            
    .take(count + 1)            
    .map(new Function() {                
         @Override                
         public Long apply(@NonNull Long aLong) throws Exception {
             return count - aLong; // 由于是倒计时,需要将倒计时的数字反过来    
         }            
     })            
     .observeOn(AndroidSchedulers.mainThread())            
     .doOnSubscribe(new Consumer() {         
          @Override                
          public void accept(@NonNull Disposable disposable) throws Exception {         
              button.setEnabled(false);                    
              button.setTextColor(Color.GRAY);               
          }            
      })            
      .subscribe(new Observer() {           
           @Override                
           public void onSubscribe(Disposable d) {                
           }                
           @Override                
           public void onNext(Long aLong) {                    
               button.setText(aLong + "秒后重发");                
           }                
           @Override                
           public void onError(Throwable e) {                
           }                
           @Override                
           public void onComplete() {                    
               button.setEnabled(true);                    
               button.setTextColor(Color.RED);                    
               button.setText("发送验证码");                
           }            
       });
 }

 

你可能感兴趣的:(Android)