效果图
android 自定义倒计时控件使用很简单
一、在xml布局文件中加载自定义控件
<com.xxx.xxxxxx.view.TimerTextView
android:id="@+id/timerTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#353535"
android:textSize="25sp"
android:layout_toRightOf="@id/recordsuspendbtn"
android:layout_marginLeft="13dp"
android:layout_marginTop="12dp"/>
二、在activity 中找到该控件,并添加计时结束的回调
TimerTextView mTimerTextView = findViewById(R.id.timerTextView);
mTimerTextView.addClick(() -> {
//计时已结束,通知handler 刷新布局
mHandler.sendEmptyMessage(RECORDTIMEOVER);
});
三、传入需要计时的总长度(毫秒),并开始计时
mTimerTextView.setMillisecond(90000);
四、在需要的地方调用暂停、停止的方法
if(mTimerTextView.isMisPause()){
//暂停
mTimerTextView.setMisPause(false);
}
//停止
mTimerTextView.stop();
注意:默认是正计时,如果要使用倒计时,需要用到 countdown() 方法,把下面的 timing() 方法注释掉
/**
* 实现计时循环
*/
@Override
public void run() {
if (isRun) {
if(!misPause){
this.setText(showTime());
//countdown(); //开始倒计时
timing(); //开始正计时
}
postDelayed(this, 1000);
} else {
removeCallbacks(this);
}
}
控件下载地址:去下载