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;
    }

}
相关推荐
FungLeo5 小时前
FengCMS · PHP+MySQL 升级实录 · 危险默认值之殇:root/123456、认证码 8888 写进配置加载器
android·mysql·php·安全加固·fengcms
yz_aiks5 小时前
Termux 使用教程:把安卓手机变成随身 Linux(装 Python、SSH 远程、跑自动化脚本)
android·linux·python·智能手机·ssh·termux
booboosui13 小时前
编号没变,消息还是来了:Mac 端通知读取漏第二条的原因
android
雨白1 天前
单例设计模式:从 Java/Kotlin 实现到 Android 实战
android
hai_android1 天前
一行 asLiveData,背后是一座生命周期的桥
android·java·kotlin
阿杰m1 天前
Android源码第二十三章:Telephony、RIL 与 IMS——来电怎么到应用?
android
booboosui1 天前
没人认领 .md:安卓把报告交给了音乐播放器打开
android
用户60709759149062 天前
AOSP 14 OTA 升级指导(四):update_engine 内部机制——Action 流水线与写盘
android
恋猫de小郭2 天前
Jetpack Compose 1.13 发布,憋了这么久终有交代了
android·前端·flutter
JMchen1232 天前
【Android 性能优化实战 60 讲】12 异步 Inflate 实战与痛击:源码拆解原生瓶颈,手写增强版布局预加载框架
android·性能优化·实战·启动优化·异步inflate·x2c