Мне нужно, чтобы он освежил экран (и время) каждую секунду. < /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
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()
}
}
}
}
}
@Composable
@RequiresApi(Build.VERSION_CODES.O)
fun UpdateTime() {
val formatter = DateTimeFormatter.ofPattern("HH:mm:ss")
val current = LocalDateTime.now().format(formatter)
TimeDisplay(
name = current.toString(),
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)
)
}
< /code>
Я пробовал петли, потоки, ручки и другие методы, которые я нашел в Google. Они входят в пост. Но основная проблема заключается в том, когда я пытаюсь позвонить в UpdateTime, он жалуется на то, что он будет композицией. val handler = Handler(Looper.getMainLooper())
val runnable = object : Runnable {
override fun run() {
UpdateTime()
// Schedule the next execution after 1 second
handler.postDelayed(this, 1000) // 1000 milliseconds = 1 second
}
}
, и если я позволю IDE добавлять @composable до переопределения Fun Run () Line, я получу дальнейшие ошибки
>
Подробнее здесь: https://stackoverflow.com/questions/797 ... ery-second
Мобильная версия