/** * 开始计时 */ public static void startTime(final TextView tvGetCode) { final long codeTimes = 60; Observable.interval(0, 1, TimeUnit.SECONDS) .take(codeTimes - 1) .map(new Function<Long, Long>() { @Override public Long apply(Long aLong) throws Exception { return codeTimes - aLong; } }) .observeOn(AndroidSchedulers.mainThread()) .doOnSubscribe(new Consumer<Disposable>() { @Override public void accept(Disposable disposable) throws Exception { tvGetCode.setEnabled(false); } }) .subscribe(new Observer<Long>() { @Override public void onSubscribe(Disposable d) { } @Override public void onNext(Long value) { tvGetCode.setText("剩余" + value + "秒"); } @Override public void onError(Throwable e) { } @Override public void onComplete() { tvGetCode.setEnabled(true); tvGetCode.setText("获取验证码"); } }); }