您现在的位置是:群英 > 开发技术 > 移动开发
Android倒计时实现方式有几种,代码怎么写
Admin发表于 2022-05-17 11:47:201182 次浏览
这篇文章给大家介绍了“Android倒计时实现方式有几种,代码怎么写”的相关知识,讲解详细,步骤过程清晰,对大家进一步学习和理解“Android倒计时实现方式有几种,代码怎么写”有一定的帮助,希望大家阅读完这篇文章能有所收获。下面就请大家跟着小编的思路一起来学习一下吧。

android 倒计时一般实现方式:

  • handler+postdelayed() 方式
  • timer + timertask + handler 方式
  • scheduledexecutorservice + handler 方式
  • rxjava 方式
  • countdowntimer 方式

现在因为有了协程和flow,我们可以借助flow这个工具,更加优雅地实现这个需求功能.

1.依赖导入

    api 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.2'
    api 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.4.1'
    // lifecyclescope(可选)
    api "androidx.lifecycle:lifecycle-runtime-ktx:2.2.0"

2. 代码实现

fun countdowncoroutines(
    total: int,
    scope: coroutinescope,
    ontick: (int) -> unit,
    onstart: (() -> unit)? = null,
    onfinish: (() -> unit)? = null,
): job {
    return flow {
        for (i in total downto 0) {
            emit(i)
            delay(1000)
        }
    }.flowon(dispatchers.main)
        .onstart { onstart?.invoke() }
        .oncompletion { onfinish?.invoke() }
        .oneach { ontick.invoke(it) }
        .launchin(scope)
}

2.1使用:

private var mcountdownjob: job? = null

mbinding.btnstart.setonclicklistener {
    mcountdownjob = countdowncoroutines(60, lifecyclescope,
        ontick = { second ->
            mbinding.text.text = "${second}s后重发"
        }, onstart = {
            // 倒计时开始
        }, onfinish = {
            // 倒计时结束,重置状态
            mbinding.text.text = "发送验证码"
        })
}
mbinding.btnstop.setonclicklistener { 
    // 取消倒计时
    mcountdownjob?.cancel() 

其他的完整demo https://github.com/dahui888/kotlinpractice

补充:

下面是小编收集整理android 实现倒计时的几种方式

使用 timer方式:

  /**
     * 开始
     */
    public void starttimer() {
        if (timer == null) {
            timer = new timer();
        }
        if (timertask == null) {
            timertask = new timertask() {
                @override
                public void run() {
                    message message = new message();
                    message.what = 2;
                    handler.sendmessage(message);
                }
            };
        }
        if (timer != null && timertask != null) {
            timer.schedule(timertask, 0, 2000);
        }
    }
    /**
     * 暂停定时器
     */
    public void stoptimer() {
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
        if (timertask != null) {
            timertask.cancel();
            timertask = null;
        }
    }

使用rxjava方式:

  private void countdown() {
        mddisposable = flowable.intervalrange(0, constant.count_down, 0,                     1,timeunit.seconds)
                .observeon(androidschedulers.mainthread())
                .doonnext((along) -> logutils.e("倒计时--" + along))
                .dooncomplete(() -> randomselectseat())
                .subscribe();
    }
    /**
     * 销毁
     */
     @override
    protected void ondestroy() {
        if (mddisposable != null) {
            mddisposable.dispose();
        }
        super.ondestroy();
    }

使用countdowntimer方式:

//倒计时countdowntimer
//每过1000毫秒执行一次ontick
//倒计时完成执行onfinish
countdowntimer timer = new countdowntimer(5000, 1000){
    @override
    public void ontick(long sin) {
        toast.maketext(mainactivity.this, "" + sin/1000, toast.length_short).show();
    }
 
    @override
    public void onfinish() {
        toast.maketext(mainactivity.this, "倒计时完成", toast.length_short).show();
    }
};
//开始
timer.start();
//暂停
if (timer != null) {
     timer.cancel();
     timer = null;
 }

到此这篇关于“Android倒计时实现方式有几种,代码怎么写”的文章就介绍到这了,感谢各位的阅读,更多相关Android倒计时实现方式有几种,代码怎么写内容,欢迎关注群英网络资讯频道,小编将为大家输出更多高质量的实用文章!

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。

标签: android 倒计时
相关信息推荐
2022-05-16 17:19:04 
摘要:表单是用来与用户做交流的一个网页控件,良好的表单设计能够让网页与用户更好的沟通。表单中常见的元素主要包括:文本输入框、下拉选择框、单选按钮、复选按钮、文本域和按钮等。
2022-05-13 17:52:24 
摘要:实现数值求和的方法:1、当只有两个数值时,直接使用“数值1+数值2”语句来求和;2、当有多个数值时,可以用“array(值1,值2,值3...值n)”语句将数值存入数组中,用“array_sum($arr)”语句计算数组中所有元素的和即可。
2021-12-04 17:46:55 
摘要:这篇文章给大家分享的是Python模块的glob模块的相关内容。glob模块也是Python标准库中一个重要的模块,小编觉得比较实用的,因此分享给大家做个参考,文中示例代码介绍的非常详细,感兴趣的朋友接下来一起跟随小编看看吧。
云活动
推荐内容
热门关键词
热门信息
群英网络助力开启安全的云计算之旅
立即注册,领取新人大礼包
  • 联系我们
  • 24小时售后:4006784567
  • 24小时TEL :0668-2555666
  • 售前咨询TEL:400-678-4567

  • 官方微信

    官方微信
Copyright  ©  QY  Network  Company  Ltd. All  Rights  Reserved. 2003-2019  群英网络  版权所有   茂名市群英网络有限公司
增值电信经营许可证 : B1.B2-20140078   粤ICP备09006778号
免费拨打  400-678-4567
免费拨打  400-678-4567 免费拨打 400-678-4567 或 0668-2555555
微信公众号
返回顶部
返回顶部 返回顶部