@@ -38,14 +38,14 @@ import androidx.compose.material.TextField
3838// noinspection UsingMaterialAndMaterial3Libraries
3939import androidx.compose.material3.Text
4040import androidx.compose.runtime.Composable
41- import androidx.compose.runtime.LaunchedEffect
4241import androidx.compose.runtime.remember
4342import androidx.compose.ui.Modifier
4443import androidx.compose.ui.graphics.Brush
4544import androidx.compose.ui.graphics.Color
4645import androidx.compose.ui.text.TextStyle
4746import androidx.compose.ui.text.font.FontWeight
4847import androidx.compose.ui.text.input.ImeAction
48+ import androidx.compose.ui.text.input.KeyboardType
4949import androidx.compose.ui.tooling.preview.Preview
5050import androidx.compose.ui.unit.dp
5151import androidx.core.text.isDigitsOnly
@@ -144,21 +144,19 @@ fun TextFieldInitialState() {
144144 // [END android_compose_state_text_7]
145145}
146146
147+ @Preview(showBackground = true )
147148@Composable
148149fun TextFieldBuffer () {
149150 // [START android_compose_state_text_8]
150- val phoneNumberState = rememberTextFieldState()
151-
152- LaunchedEffect (phoneNumberState) {
153- phoneNumberState.edit { // TextFieldBuffer scope
154- append(" 123456789" )
155- }
156- }
151+ val phoneNumberState = rememberTextFieldState(" 1234567890" )
157152
158153 TextField (
159154 state = phoneNumberState,
160- inputTransformation = InputTransformation { // TextFieldBuffer scope
161- if (asCharSequence().isDigitsOnly()) {
155+ keyboardOptions = KeyboardOptions (
156+ keyboardType = KeyboardType .Phone
157+ ),
158+ inputTransformation = InputTransformation .maxLength(10 ).then {
159+ if (! asCharSequence().isDigitsOnly()) {
162160 revertAllChanges()
163161 }
164162 },
@@ -174,32 +172,37 @@ fun TextFieldBuffer() {
174172@Preview
175173@Composable
176174fun EditTextFieldState () {
177- // [START android_compose_state_text_9]
178175 val usernameState = rememberTextFieldState(" I love Android" )
176+ editTFState(usernameState)
177+ }
178+
179+ fun editTFState (textFieldState : TextFieldState ) {
180+ // [START android_compose_state_text_9]
181+ // Initial textFieldState text passed in is "I love Android"
179182 // textFieldState.text : I love Android
180183 // textFieldState.selection: TextRange(14, 14)
181- usernameState .edit { insert(14 , " !" ) }
184+ textFieldState .edit { insert(14 , " !" ) }
182185 // textFieldState.text : I love Android!
183186 // textFieldState.selection: TextRange(15, 15)
184- usernameState .edit { replace(7 , 14 , " Compose" ) }
187+ textFieldState .edit { replace(7 , 14 , " Compose" ) }
185188 // textFieldState.text : I love Compose!
186189 // textFieldState.selection: TextRange(15, 15)
187- usernameState .edit { append(" !!!" ) }
190+ textFieldState .edit { append(" !!!" ) }
188191 // textFieldState.text : I love Compose!!!!
189192 // textFieldState.selection: TextRange(18, 18)
190- usernameState .edit { selectAll() }
193+ textFieldState .edit { selectAll() }
191194 // textFieldState.text : I love Compose!!!!
192195 // textFieldState.selection: TextRange(0, 18)
193196 // [END android_compose_state_text_9]
194197
195198 // [START android_compose_state_text_10]
196- usernameState .setTextAndPlaceCursorAtEnd(" I really love Android" )
199+ textFieldState .setTextAndPlaceCursorAtEnd(" I really love Android" )
197200 // textFieldState.text : I really love Android
198201 // textFieldState.selection : TextRange(21, 21)
199202 // [END android_compose_state_text_10]
200203
201204 // [START android_compose_state_text_11]
202- usernameState .clearText()
205+ textFieldState .clearText()
203206 // textFieldState.text :
204207 // textFieldState.selection : TextRange(0, 0)
205208 // [END android_compose_state_text_11]
0 commit comments