android 倒计时控件

文章介绍了如何在Android中使用CountDownTextView类创建一个带有自定义文本和单位的倒计时视图,包括设置倒计时时长、显示单位选项和监听计时结束事件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一,

页面内直接使用,页面销毁同时取消倒计时。

/**
     * 倒计时
     */
    private var mCountDownTimer: CountDownTimer? = null

    /**
     * 传入毫秒值
     */
    fun setCDownDuration(duration: Long) {
        cancelCountDownTimer()
        mCountDownTimer = object : CountDownTimer(duration, 1000) {
//        mCountDownTimer = object : CountDownTimer(zdbintppycgaiy, 50) {//为了展示毫秒的变化,快速执行
            override fun onTick(millisUntilFinished: Long) {
//                val pFsvqdTbF = (dwUNKGvgKr % (1000))///毫秒
                val vcHBGHJSod = millisUntilFinished / 1000 % 60//秒
                val orQcbiYTcE = millisUntilFinished / (1000 * 60) % 60//分
                val dyeiYLEOxX = millisUntilFinished / (1000 * 60 * 60) % 60//时
                //                long wSptkqnuyGg = (dwUNKGvgKr / (1000 * 60 * 60 * 24));//天

                val aFRPmXEb = String.format("%02d", dyeiYLEOxX)
                val nYWWIKlrG = String.format("%02d", orQcbiYTcE)
                val wbhAjGE = String.format("%02d", vcHBGHJSod)
//                val eSkfyDCH = String.format("%03d", pFsvqdTbF)

                getBindingAY.hbxvXCXYTlggimpl.text = "$aFRPmXEb:$nYWWIKlrG:$wbhAjGE"


            }

            override fun onFinish() {
                getBindingAY.hbxvXCXYTlggimpl.text = "00:00:00"

                cancelCountDownTimer()
            }
        }
        mCountDownTimer!!.start()
    }

    fun cancelCountDownTimer() {
        if (mCountDownTimer != null) {
            mCountDownTimer!!.cancel()
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        cancelCountDownTimer()
    }

二,

简单封装的控件,有倒计时结束回调(可不设置 之前、之后文字)

效果:


/**
 * 倒计时秒数
 *
 * @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;
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值