一、效果图
二、extendcontext.kt 文件代码
注意:创建extendcontext.kt选择file
使用kotlin扩展方法的特性创建countdown扩展方法,避免多个地方使用倒计时重复创建countdown方法
package com.example.baselib.extension import androidx.fragment.app.fragmentactivity import androidx.lifecycle.lifecyclescope import kotlinx.coroutines.coroutinescope import kotlinx.coroutines.delay import kotlinx.coroutines.flow.catch import kotlinx.coroutines.flow.flow import kotlinx.coroutines.flow.oncompletion import kotlinx.coroutines.flow.onstart import kotlinx.coroutines.launch fun fragmentactivity.countdown( timemillis: long = 1000,//默认时间间隔 1 秒 time: int = 3,//默认时间为 3 秒 start: (scop: coroutinescope) -> unit, end: () -> unit, next: (time: int) -> unit, error: (msg: string?) -> unit ) { lifecyclescope.launch { flow { (time downto 1).foreach { delay(timemillis) emit(it) } }.onstart { start(this@launch) }.oncompletion { end() }.catch { error(it.message ?: "countdown 出现未知错误") }.collect { next(it) } } }
三、mainactivity.kt代码
package com.example.testkotlin import androidx.appcompat.app.appcompatactivity import android.os.bundle import android.util.log import com.example.baselib.extension.countdown import kotlinx.coroutines.coroutinescope import kotlinx.coroutines.cancel class mainactivity : appcompatactivity() { private var mcountdown: coroutinescope? = null override fun oncreate(savedinstancestate: bundle?) { super.oncreate(savedinstancestate) setcontentview(r.layout.activity_main) loaddata() } private fun loaddata() { this.countdown(time = 5, start = { mcountdown = it }, end = { log.i("==", "倒计时结束了") }, next = { log.i("==", "剩余 $it 秒") }, error = { }) } override fun ondestroy() { mcountdown?.let { it.cancel() } super.ondestroy() } }
四、build.gradle.kts代码
plugins { id("com.android.application") id("org.jetbrains.kotlin.android") } android { namespace = "com.example.testkotlin" compilesdk = 34 defaultconfig { applicationid = "com.example.testkotlin" minsdk = 23 targetsdk = 34 versioncode = 1 versionname = "1.0" testinstrumentationrunner = "androidx.test.runner.androidjunitrunner" } buildtypes { release { isminifyenabled = false proguardfiles( getdefaultproguardfile("proguard-android-optimize.txt"), "proguard-rules.pro" ) } } compileoptions { sourcecompatibility = javaversion.version_1_8 targetcompatibility = javaversion.version_1_8 } kotlinoptions { jvmtarget = "1.8" } } dependencies { implementation("androidx.core:core-ktx:1.9.0") implementation("androidx.appcompat:appcompat:1.6.1") implementation("com.google.android.material:material:1.11.0") implementation("androidx.constraintlayout:constraintlayout:2.1.4") testimplementation("junit:junit:4.13.2") androidtestimplementation("androidx.test.ext:junit:1.1.5") androidtestimplementation("androidx.test.espresso:espresso-core:3.5.1") }
扩展:
kotlin——倒计时(countdowntimer和flow形式)
一、kotlin倒计时-谷歌countdowntimer
简介:谷歌官方推荐使用countdowntimer,非常的简单好用,代码也很少
代码
var totaltime : long = 2*60*60*1000 //总时长 2小时 var countdowntimer=object : countdowntimer(totaltime,1000){//1000ms运行一次ontick里面的方法 override fun onfinish() { log.d(tag,"==倒计时结束") } override fun ontick(millisuntilfinished: long) { var hour=millisuntilfinished/1000/60/60 var minute=millisuntilfinished/1000/60` var second=millisuntilfinished/1000` log.d(tag,"==倒计时" hour "小时" minute "分" second "秒") } }.start()
使用说明
1、把这段代码放到类的属性里就可以自动运行了
2、把上面代码中的.start()去掉,然后在需要用到的地方countdowntimer.start()就可以运行了
二、kotlin倒计时-谷歌countdowntimer
引入协程:implementation ‘androidx.lifecycle:lifecycle-runtime-ktx:2.2.0’ // 协程lifecyclescope
倒计时代码(扩展方法):
import android.util.log import androidx.appcompat.app.appcompatactivity import androidx.lifecycle.lifecyclescope import kotlinx.coroutines.coroutinescope import kotlinx.coroutines.delay import kotlinx.coroutines.flow.catch import kotlinx.coroutines.flow.* import kotlinx.coroutines.flow.oncompletion import kotlinx.coroutines.flow.onstart import kotlinx.coroutines.launch /** * 扩展方法 * 倒计时的实现 */ fun appcompatactivity.countdown( time: int = 30, start: (scop: coroutinescope) -> unit, next: (time: string) -> unit, end: () -> unit ) { lifecyclescope.launch { // 在这个范围内启动的协程会在lifecycle被销毁的时候自动取消 flow { (time downto 0).foreach { delay(1000) emit(it) } }.onstart { // 倒计时开始 ,在这里可以让button 禁止点击状态 start(this@launch) }.oncompletion { // 倒计时结束 ,在这里可以让button 恢复点击状态 end() }.catch { //错误 log.e("", it.message ?: "unkown error") }.collect { // 在这里 更新值来显示到ui next(it.tostring()) } } }