Skip to content

Commit 5e4f8a9

Browse files
committed
Update StateBasedText.kt
1 parent c2c0ffe commit 5e4f8a9

File tree

1 file changed

+26
-30
lines changed

1 file changed

+26
-30
lines changed

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

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import androidx.compose.ui.text.input.ImeAction
4848
import androidx.compose.ui.tooling.preview.Preview
4949
import androidx.compose.ui.unit.dp
5050
import androidx.lifecycle.ViewModel
51+
import androidx.core.text.isDigitsOnly
5152

5253
@Composable
5354
fun StateBasedTextSnippets() {
@@ -114,15 +115,6 @@ fun StateHoisting() {
114115
// [END android_compose_state_text_6]
115116
}
116117

117-
class Repository {
118-
fun login(username: String, password: String) {
119-
}
120-
}
121-
122-
@Composable
123-
fun TextFieldPlaceholder() {
124-
}
125-
126118
@Composable
127119
fun TextFieldInitialState() {
128120
// [START android_compose_state_text_7]
@@ -147,7 +139,7 @@ fun TextFieldBuffer() {
147139
TextField(
148140
state = phoneNumberState,
149141
inputTransformation = InputTransformation { // TextFieldBuffer scope
150-
if (TextUtils.isDigitsOnly(asCharSequence())) {
142+
if (asCharSequence().isDigitsOnly()) {
151143
revertAllChanges()
152144
}
153145
},
@@ -163,7 +155,7 @@ fun TextFieldBuffer() {
163155
@Preview
164156
@Composable
165157
fun EditTextFieldState() {
166-
// [START android_compose_state_text_10]
158+
// [START android_compose_state_text_9]
167159
val usernameState = rememberTextFieldState("I love Android")
168160
// textFieldState.text : I love Android
169161
// textFieldState.selection: TextRange(14, 14)
@@ -179,19 +171,19 @@ fun EditTextFieldState() {
179171
usernameState.edit { selectAll() }
180172
// textFieldState.text : I love Compose!!!!
181173
// textFieldState.selection: TextRange(0, 18)
182-
// [END android_compose_state_text_10]
174+
// [END android_compose_state_text_9]
183175

184-
// [START android_compose_state_text_11]
176+
// [START android_compose_state_text_10]
185177
usernameState.setTextAndPlaceCursorAtEnd("I really love Android")
186178
// textFieldState.text : I really love Android
187179
// textFieldState.selection : TextRange(21, 21)
188-
// [END android_compose_state_text_11]
180+
// [END android_compose_state_text_10]
189181

190-
// [START android_compose_state_text_12]
182+
// [START android_compose_state_text_11]
191183
usernameState.clearText()
192184
// textFieldState.text :
193185
// textFieldState.selection : TextRange(0, 0)
194-
// [END android_compose_state_text_12]
186+
// [END android_compose_state_text_11]
195187
}
196188

197189
class TextFieldViewModel : ViewModel() {
@@ -224,14 +216,6 @@ fun TextFieldInputTransformation() {
224216
inputTransformation = InputTransformation.maxLength(10)
225217
)
226218
// [END android_compose_state_text_14]
227-
228-
// [START android_compose_state_text_17]
229-
TextField(
230-
state = rememberTextFieldState(),
231-
inputTransformation = InputTransformation.maxLength(6)
232-
.then(CustomInputTransformation()),
233-
)
234-
// [END android_compose_state_text_17]
235219
}
236220

237221
// [START android_compose_state_text_15]
@@ -251,29 +235,41 @@ class DigitOnlyInputTransformation : InputTransformation {
251235
}
252236
// [END android_compose_state_text_16]
253237

254-
// [START android_compose_state_text_17]
238+
@Composable
239+
fun ChainInputTransformation() {
240+
// [START android_compose_state_text_17]
241+
TextField(
242+
state = rememberTextFieldState(),
243+
inputTransformation = InputTransformation.maxLength(6)
244+
.then(CustomInputTransformation()),
245+
)
246+
// [END android_compose_state_text_17]
247+
}
248+
249+
// [START android_compose_state_text_18]
255250
class CustomOutputTransformation : OutputTransformation {
256251
override fun TextFieldBuffer.transformOutput() {
252+
257253
}
258254
}
259-
// [END android_compose_state_text_17]
255+
// [END android_compose_state_text_18]
260256

261-
// [START android_compose_state_text_18]
257+
// [START android_compose_state_text_19]
262258
class PhoneNumberOutputTransformation : OutputTransformation {
263259
override fun TextFieldBuffer.transformOutput() {
264260
if (length > 0) insert(0, "(")
265261
if (length > 4) insert(4, ")")
266262
if (length > 8) insert(8, "-")
267263
}
268264
}
269-
// [END android_compose_state_text_18]
265+
// [END android_compose_state_text_19]
270266

271267
@Composable
272268
fun TextFieldOutputTransformation() {
273-
// [START android_compose_state_text_19]
269+
// [START android_compose_state_text_20]
274270
TextField(
275271
state = rememberTextFieldState(),
276272
outputTransformation = PhoneNumberOutputTransformation()
277273
)
278-
// [END android_compose_state_text_19]
274+
// [END android_compose_state_text_20]
279275
}

0 commit comments

Comments
 (0)