如何在Android Studio的Kotlin中实现延迟1秒后继续执行代码(附Godot GDScript示例对比)
Got it! You mentioned using yield(get_tree().create_timer(1),"timeout") in Godot's GDScript to pause execution for 1 second before running subsequent code. In Kotlin for Android, there are a few clean ways to achieve this same behavior, with coroutines being the most modern and recommended approach (since it lets you write sequential-looking code just like Godot's yield).
1. Using Kotlin Coroutines (Recommended)
Coroutines let you write asynchronous code that reads like synchronous code, which matches the intuitive flow you're used to with Godot's yield. Here's how to do it:
First, make sure you have the coroutines dependency in your module-level build.gradle file:
dependencies { implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.3" }
Then, in your Activity/Fragment/ViewModel, use a lifecycle-aware scope (like lifecycleScope for Activities/Fragments) to run the delay—this ensures the coroutine gets cleaned up automatically when the component is destroyed, avoiding memory leaks:
// Inside an Activity or Fragment lifecycleScope.launch { // Code that runs immediately println("Starting the 1-second wait...") // Pause execution for 1 second (1000 milliseconds) delay(1000) // Code to run AFTER the delay println("1 second passed! Executing next logic...") // Add your actual work here—like updating UI, calling an API, or triggering a function }
This approach mirrors Godot's yield perfectly: your code flows line-by-line, no messy nested callbacks required.
2. Using Handler (Legacy Approach)
If you're working with an older codebase or prefer a more traditional method, you can use Handler:
// Get a Handler tied to the main thread (critical for UI updates) val handler = Handler(Looper.getMainLooper()) // Code before the delay println("Starting wait with Handler...") // Post a task to run after 1 second handler.postDelayed({ // Code to execute after the delay println("1 second passed via Handler!") // Your logic here }, 1000) // Delay duration in milliseconds
Note: On Android 10 (API 29+) and above, the empty Handler() constructor is deprecated, so always pass Looper.getMainLooper() to ensure your code runs on the main thread.
3. Using View.postDelayed (For View-Specific Scenarios)
If you're working directly with a View (like a button click listener), you can use the View's built-in postDelayed method:
myButton.setOnClickListener { // Code that runs right when the button is clicked println("Button clicked, waiting 1 second...") myButton.postDelayed({ // Code to run after 1 second println("1 second passed—running button action!") }, 1000) }
This is handy because it automatically uses the main thread and is tied to the View's lifecycle, so the task is canceled if the View is destroyed.
Quick Comparison to Godot's Yield
- Godot's
yieldpauses the current function until the timer times out. - Kotlin's
delay(1000)in a coroutine does the exact same thing: it pauses the coroutine (not the entire app) for 1 second, then resumes execution of the code that comes after it—keeping your code flow clean and linear, just like you're used to in GDScript.
内容的提问来源于stack exchange,提问作者Serdar Çivi

