Files
AndroidJetpack/Jetpack-Compose-Playground/docs/cookbook/textfield_changes.md
T
2026-07-15 16:57:36 +08:00

36 lines
1.1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!---
This is the API of version 1.2.0
-->
# Handle changes to a TextField
In some cases, its useful to get the value of a textfield every time the text in a text field changes. For example, you might want to build a search screen with autocomplete functionality where you want to update the results as the user types.
Here is an example how you can do it with Compose:
<p align="left">
<img src ="../../images/TextFieldDemo.png" />
</p>
```kotlin
@Composable
fun TextFieldDemo() {
Column(Modifier.padding(16.dp)) {
val textState = remember { mutableStateOf(TextFieldValue()) }
TextField(
value = textState.value,
onValueChange = { textState.value = it }
)
Text("The textfield has this text: " + textState.value.text)
}
}
```
The simplest approach is to supply an onValueChange() callback to a TextField. Whenever the text changes, the callback is invoked.
In this example, every time the TextField changes, the new text value will be saved in a state and set to the TextField and the Text.
## See also:
* [Full Example Code]({{ site.samplefolder }}/material/textfield/TextFieldDemo.kt)