Skip to content

Commit 3e9db06

Browse files
committed
Update StateBasedText.kt
1 parent 89ecf71 commit 3e9db06

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed

compose/snippets/src/main/java/com/example/compose/snippets/text/StateBasedText.kt

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import androidx.compose.foundation.layout.Column
1919
import androidx.compose.foundation.layout.Spacer
2020
import androidx.compose.foundation.layout.height
2121
import androidx.compose.foundation.layout.padding
22+
import androidx.compose.foundation.text.BasicTextField
2223
import androidx.compose.foundation.text.KeyboardOptions
2324
import androidx.compose.foundation.text.input.InputTransformation
2425
import androidx.compose.foundation.text.input.OutputTransformation
@@ -144,21 +145,16 @@ fun TextFieldInitialState() {
144145
// [END android_compose_state_text_7]
145146
}
146147

148+
@Preview(showBackground = true)
147149
@Composable
148150
fun TextFieldBuffer() {
149151
// [START android_compose_state_text_8]
150-
val phoneNumberState = rememberTextFieldState()
151-
152-
LaunchedEffect(phoneNumberState) {
153-
phoneNumberState.edit { // TextFieldBuffer scope
154-
append("123456789")
155-
}
156-
}
152+
val phoneNumberState = rememberTextFieldState("123456789")
157153

158154
TextField(
159155
state = phoneNumberState,
160156
inputTransformation = InputTransformation { // TextFieldBuffer scope
161-
if (asCharSequence().isDigitsOnly()) {
157+
if (!asCharSequence().isDigitsOnly()) {
162158
revertAllChanges()
163159
}
164160
},
@@ -174,32 +170,37 @@ fun TextFieldBuffer() {
174170
@Preview
175171
@Composable
176172
fun EditTextFieldState() {
177-
// [START android_compose_state_text_9]
173+
Text("Happy Cat")
178174
val usernameState = rememberTextFieldState("I love Android")
175+
editTFState(usernameState)
176+
}
177+
178+
fun editTFState(textFieldState: TextFieldState){
179+
// [START android_compose_state_text_9]
179180
// textFieldState.text : I love Android
180181
// textFieldState.selection: TextRange(14, 14)
181-
usernameState.edit { insert(14, "!") }
182+
textFieldState.edit { insert(14, "!") }
182183
// textFieldState.text : I love Android!
183184
// textFieldState.selection: TextRange(15, 15)
184-
usernameState.edit { replace(7, 14, "Compose") }
185+
textFieldState.edit { replace(7, 14, "Compose") }
185186
// textFieldState.text : I love Compose!
186187
// textFieldState.selection: TextRange(15, 15)
187-
usernameState.edit { append("!!!") }
188+
textFieldState.edit { append("!!!") }
188189
// textFieldState.text : I love Compose!!!!
189190
// textFieldState.selection: TextRange(18, 18)
190-
usernameState.edit { selectAll() }
191+
textFieldState.edit { selectAll() }
191192
// textFieldState.text : I love Compose!!!!
192193
// textFieldState.selection: TextRange(0, 18)
193194
// [END android_compose_state_text_9]
194195

195196
// [START android_compose_state_text_10]
196-
usernameState.setTextAndPlaceCursorAtEnd("I really love Android")
197+
textFieldState.setTextAndPlaceCursorAtEnd("I really love Android")
197198
// textFieldState.text : I really love Android
198199
// textFieldState.selection : TextRange(21, 21)
199200
// [END android_compose_state_text_10]
200201

201202
// [START android_compose_state_text_11]
202-
usernameState.clearText()
203+
textFieldState.clearText()
203204
// textFieldState.text :
204205
// textFieldState.selection : TextRange(0, 0)
205206
// [END android_compose_state_text_11]

0 commit comments

Comments
 (0)