使用协程的带有水平进度条的启动画面
Splash screen with horizontal progressBar with using coroutines
我正在尝试使用 progressBar 创建闪屏。我想使用协程创建它。这是我的代码:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.splash_screen)
progressBarLoading = findViewById(R.id.loadingProgress)
lifecycleScope.launch {
delay(1000)
if (progressBarStatus < 100) {
progressBarLoading?.setProgress(progressBarStatus)
progressBarStatus = progressBarStatus + 30
} else {
var intent = Intent(this@SplashActivity, MainActivity::class.java)
startActivity(intent)
}
}
}
但是当启动画面显示时,进度条没有移动并且没有切换到第二个屏幕。我不明白哪里出了问题。
您只调用了一次代码。你需要做类似
的事情
lifecycleScope.launch {
while(true){
delay(1000)
if (progressBarStatus < 100) {
progressBarLoading?.setProgress(progressBarStatus)
progressBarStatus += 30
} else {
var intent = Intent(this@SplashActivity, MainActivity::class.java)
startActivity(intent)
finish()
break
}
}
}
我正在尝试使用 progressBar 创建闪屏。我想使用协程创建它。这是我的代码:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.splash_screen)
progressBarLoading = findViewById(R.id.loadingProgress)
lifecycleScope.launch {
delay(1000)
if (progressBarStatus < 100) {
progressBarLoading?.setProgress(progressBarStatus)
progressBarStatus = progressBarStatus + 30
} else {
var intent = Intent(this@SplashActivity, MainActivity::class.java)
startActivity(intent)
}
}
}
但是当启动画面显示时,进度条没有移动并且没有切换到第二个屏幕。我不明白哪里出了问题。
您只调用了一次代码。你需要做类似
的事情lifecycleScope.launch {
while(true){
delay(1000)
if (progressBarStatus < 100) {
progressBarLoading?.setProgress(progressBarStatus)
progressBarStatus += 30
} else {
var intent = Intent(this@SplashActivity, MainActivity::class.java)
startActivity(intent)
finish()
break
}
}
}