Мне нужно, чтобы он освежил экран (и время) каждую секунду. < /p>
Вот полный код. Теперь с изменениями, предложенными из ответа. < /P>
Код: Выделить всё
package com.example.talkingclockversion1
import android.os.Build
import android.os.Bundle
import android.view.View
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.annotation.RequiresApi
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.sp
import com.example.talkingclockversion1.ui.theme.TalkingClockVersion1Theme
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import androidx.lifecycle.ViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlin.time.Duration.Companion.seconds
class MainActivity : ComponentActivity() {
@RequiresApi(Build.VERSION_CODES.O)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContent {
TalkingClockVersion1Theme {
Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
// Hide both the navigation bar and the status bar.
window.decorView.apply {
systemUiVisibility =
View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_FULLSCREEN
}
SetScreenBackgroundToBlack()
UpdateTime()
}
}
}
}
}
@RequiresApi(Build.VERSION_CODES.O)
class TimeViewModel : ViewModel() {
@RequiresApi(Build.VERSION_CODES.O)
private val formatter = DateTimeFormatter.ofPattern("HH:mm:ss")
private val _current = MutableStateFlow("")
val current = _current.asStateFlow()
init {
viewModelScope.launch {
while (true) {
tick()
delay(1.seconds)
}
}
}
@RequiresApi(Build.VERSION_CODES.O)
private fun tick() {
_current.value = LocalDateTime.now().format(formatter)
}
}
@Composable
@RequiresApi(Build.VERSION_CODES.O)
fun UpdateTime() {
val viewModel: TimeViewModel = viewModel()
val current by viewModel.current.collectAsStateWithLifecycle()
TimeDisplay(
name = current,
modifier = Modifier.fillMaxWidth()
)
}
@Composable
fun SetScreenBackgroundToBlack() {
Box(
modifier = Modifier
.fillMaxSize()
.background(Color.Black)
)
}
@Composable
fun TimeDisplay(name: String, modifier: Modifier = Modifier) {
Text(
text = "$name",
color = Color.White,
fontSize = 300.sp,
lineHeight = 500.sp,
textAlign = TextAlign.Center,
modifier = modifier.background(Color.Black)
)
}
Я только что получил код и код, и я только что получил код, и я только что получил код, и я. практики.
Спасибо за помощь.
Подробнее здесь: https://stackoverflow.com/questions/797 ... ery-second
Мобильная версия