一、效果图
二、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%60 var second=millisUntilFinished/1000%60 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()) } } }