android 倒计时控件

效果:(可不设置 之前、之后文字)

java 复制代码
/**
 * 倒计时秒数
 *
 * @desc : 时分秒倒计时view
 * 
 * 布局里引用后,
 *     private fun testMethod(){
 *         binding.test.setCDownStarText("之前的文字")
 *         binding.test.setCDownEndText("之后的文字")
 *         binding.test.setCDownDuration(86400*4*1000)//4天
 *         binding.test.setShowUnit(true)//显示倒计时 单位
 *         binding.test.setCountDownFinishListener {
 *             Log.e("测试","倒计时完成")
 *         }
 *     }
 */
public class CountDownTextView extends AppCompatTextView {

    //是否显示倒计时单位,默认不展示
    private boolean mShowUnit;
    private CountDownTimer mCountDownTimer;
    //倒计时 前方展示的文字
    private String mCDownStarText = "";
    //倒计时 后方展示的文字
    private String mCDownEndText = "";
    //倒计时文字 颜色
    private int mCDownTextColor = getResources().getColor(R.color.jiuytguhygtf_rvtygtfrdx_purphuygtyppp);


    public CountDownTextView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
    }


    public void setCDownDuration(long duration) {
        if (mCountDownTimer != null) {
            mCountDownTimer.cancel();
        }

        mCountDownTimer = new CountDownTimer(duration, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                long secondsNum = (millisUntilFinished / 1000) % 60;
                long minutesNum = (millisUntilFinished / (1000 * 60)) % 60;
                long hourNum = (millisUntilFinished / (1000 * 60 * 60)) % 24;
                long dayNum = (millisUntilFinished / (1000 * 60 * 60 * 24));

//                Log.e("测试", "毫秒值 =" + millisUntilFinished + " 天 =" + dayNum + " 时 =" + hourNum + " 分 =" + minutesNum + " 秒 =" + secondsNum);

                String countDownText = "";//天时分秒
                if (mShowUnit) {
                    //-------------------国内简单使用----------------------
                    countDownText = String.format("%d天%02d小时%02d分%02d秒", dayNum, hourNum, minutesNum, secondsNum);//时分秒

//                    //-------------------国际化使用----------------------
//                    String day = Kwxecrvtbygtfrdesdrf.Companion.get().getString(R.string.day);
//                    String hour = Kwxecrvtbygtfrdesdrf.Companion.get().getString(R.string.hour);
//                    String minutes = Kwxecrvtbygtfrdesdrf.Companion.get().getString(R.string.minutes);
//                    String seconds = Kwxecrvtbygtfrdesdrf.Companion.get().getString(R.string.seconds);
//
//                    countDownText = String.format(("%d" + day + "%02d" + hour + "%02d" + minutes + "%02d" + seconds), dayNum, hourNum, minutesNum, secondsNum);//时分秒
//                    //-------------------国际化使用----------------------
                } else {
                    countDownText = String.format("%d:%02d:%02d:%02d", dayNum, hourNum, minutesNum, secondsNum);//时分秒
                }
                SpannableStringBuilder spannableSb = new SpannableStringBuilder(mCDownStarText + countDownText + mCDownEndText);
                ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(mCDownTextColor);
                spannableSb.setSpan(foregroundColorSpan, mCDownStarText.length(), mCDownStarText.length() + countDownText.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

                setText(spannableSb);
            }

            @Override
            public void onFinish() {
                setText(mCDownStarText + mCDownEndText);

                if (mCountDownFinishListener != null) {
                    mCountDownFinishListener.countDownFinishListener();
                }
                if (mCountDownTimer != null) {
                    mCountDownTimer.cancel();
                }
            }
        };

        mCountDownTimer.start();
    }

    /**
     * 是否显示倒计时单位
     *
     * @param showUnit
     */
    public void setShowUnit(boolean showUnit) {
        mShowUnit = showUnit;
    }

    /**
     * 倒计时文字 颜色
     *
     * @param textColor
     */
    public void setCDownTextColor(int textColor) {
        mCDownTextColor = textColor;
    }

    /**
     * 倒计时 前方展示的文字(可不设置)
     *
     * @param starText
     */
    public void setCDownStarText(String starText) {
        mCDownStarText = starText;
    }

    /**
     * 倒计时 后方展示的文字(可不设置)
     *
     * @param endText
     */
    public void setCDownEndText(String endText) {
        mCDownEndText = endText;
    }

    /**
     * 倒计时完成监听回调
     */
    private CountDownFinishListener mCountDownFinishListener;

    public interface CountDownFinishListener {
        void countDownFinishListener();
    }

    public void setCountDownFinishListener(CountDownFinishListener listener) {
        this.mCountDownFinishListener = listener;
    }

}
相关推荐
IT乐手27 分钟前
Android 获取定位信息工具类
android
yangjunjin35 分钟前
Android ANR的解决方案
android
低调小一1 小时前
Android Gradle 的 compileOptions 与 Kotlin jvmTarget 全面理解(含案例)
android·开发语言·kotlin
苦学编程啊5 小时前
【2025Flutter 入门指南】Dart SDK 安装与 VS Code 环境配置-Windows
android·dart
yuanManGan11 小时前
走进Linux的世界:初识操作系统(Operator System)
android·linux·运维
叶羽西11 小时前
Android15跟踪函数调用关系
android
消失的旧时光-194313 小时前
webView 的canGoBack/goBack 回退栈
android·webview
SHEN_ZIYUAN13 小时前
Flow 责任链模式图解
android
沐怡旸15 小时前
【底层机制】LeakCanary深度解析:从对象监控到内存泄漏分析的完整技术体系
android·面试
又菜又爱coding15 小时前
Android + Flutter打包出来的APK体积太大
android·flutter