@@ -38,14 +38,14 @@ import androidx.compose.material.TextField
38
38
// noinspection UsingMaterialAndMaterial3Libraries
39
39
import androidx.compose.material3.Text
40
40
import androidx.compose.runtime.Composable
41
- import androidx.compose.runtime.LaunchedEffect
42
41
import androidx.compose.runtime.remember
43
42
import androidx.compose.ui.Modifier
44
43
import androidx.compose.ui.graphics.Brush
45
44
import androidx.compose.ui.graphics.Color
46
45
import androidx.compose.ui.text.TextStyle
47
46
import androidx.compose.ui.text.font.FontWeight
48
47
import androidx.compose.ui.text.input.ImeAction
48
+ import androidx.compose.ui.text.input.KeyboardType
49
49
import androidx.compose.ui.tooling.preview.Preview
50
50
import androidx.compose.ui.unit.dp
51
51
import androidx.core.text.isDigitsOnly
@@ -144,21 +144,19 @@ fun TextFieldInitialState() {
144
144
// [END android_compose_state_text_7]
145
145
}
146
146
147
+ @Preview(showBackground = true )
147
148
@Composable
148
149
fun TextFieldBuffer () {
149
150
// [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" )
157
152
158
153
TextField (
159
154
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()) {
162
160
revertAllChanges()
163
161
}
164
162
},
@@ -174,32 +172,37 @@ fun TextFieldBuffer() {
174
172
@Preview
175
173
@Composable
176
174
fun EditTextFieldState () {
177
- // [START android_compose_state_text_9]
178
175
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"
179
182
// textFieldState.text : I love Android
180
183
// textFieldState.selection: TextRange(14, 14)
181
- usernameState .edit { insert(14 , " !" ) }
184
+ textFieldState .edit { insert(14 , " !" ) }
182
185
// textFieldState.text : I love Android!
183
186
// textFieldState.selection: TextRange(15, 15)
184
- usernameState .edit { replace(7 , 14 , " Compose" ) }
187
+ textFieldState .edit { replace(7 , 14 , " Compose" ) }
185
188
// textFieldState.text : I love Compose!
186
189
// textFieldState.selection: TextRange(15, 15)
187
- usernameState .edit { append(" !!!" ) }
190
+ textFieldState .edit { append(" !!!" ) }
188
191
// textFieldState.text : I love Compose!!!!
189
192
// textFieldState.selection: TextRange(18, 18)
190
- usernameState .edit { selectAll() }
193
+ textFieldState .edit { selectAll() }
191
194
// textFieldState.text : I love Compose!!!!
192
195
// textFieldState.selection: TextRange(0, 18)
193
196
// [END android_compose_state_text_9]
194
197
195
198
// [START android_compose_state_text_10]
196
- usernameState .setTextAndPlaceCursorAtEnd(" I really love Android" )
199
+ textFieldState .setTextAndPlaceCursorAtEnd(" I really love Android" )
197
200
// textFieldState.text : I really love Android
198
201
// textFieldState.selection : TextRange(21, 21)
199
202
// [END android_compose_state_text_10]
200
203
201
204
// [START android_compose_state_text_11]
202
- usernameState .clearText()
205
+ textFieldState .clearText()
203
206
// textFieldState.text :
204
207
// textFieldState.selection : TextRange(0, 0)
205
208
// [END android_compose_state_text_11]
0 commit comments