Repository navigation
Expand file tree
/
Copy pathStateBasedText.kt
More file actions
296 lines (272 loc) · 9.5 KB
/
Copy pathStateBasedText.kt
File metadata and controls
296 lines (272 loc) · 9.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*
* Copyright 2025 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.compose.snippets.text
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.foundation.text.input.InputTransformation
import androidx.compose.foundation.text.input.OutputTransformation
import androidx.compose.foundation.text.input.TextFieldBuffer
import androidx.compose.foundation.text.input.TextFieldLineLimits
import androidx.compose.foundation.text.input.TextFieldState
import androidx.compose.foundation.text.input.clearText
import androidx.compose.foundation.text.input.insert
import androidx.compose.foundation.text.input.maxLength
import androidx.compose.foundation.text.input.rememberTextFieldState
import androidx.compose.foundation.text.input.selectAll
import androidx.compose.foundation.text.input.setTextAndPlaceCursorAtEnd
import androidx.compose.foundation.text.input.then
import androidx.compose.material.OutlinedTextField
//noinspection UsingMaterialAndMaterial3Libraries
import androidx.compose.material.TextField
//noinspection UsingMaterialAndMaterial3Libraries
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.core.text.isDigitsOnly
import androidx.lifecycle.ViewModel
@Preview
@Composable
fun StateBasedTextSnippets() {
Column() {
// [START android_compose_state_text_0]
TextField(
state = rememberTextFieldState(initialText = "Hello"),
label = { Text("Label") }
)
// [END android_compose_state_text_0]
// [START android_compose_state_text_1]
OutlinedTextField(
state = rememberTextFieldState(),
label = { Text("Label") }
)
// [END android_compose_state_text_1]
}
}
@Preview
@Composable
fun StyleTextField() {
// [START android_compose_state_text_2]
TextField(
state = rememberTextFieldState("Hello\nWorld\nInvisible"),
lineLimits = TextFieldLineLimits.MultiLine(maxHeightInLines = 2),
placeholder = { Text("") },
textStyle = TextStyle(color = Color.Blue, fontWeight = FontWeight.Bold),
label = { Text("Enter text") },
modifier = Modifier.padding(20.dp)
)
// [END android_compose_state_text_2]
}
@Composable
fun ConfigureLineLimits() {
// [START android_compose_state_text_3]
TextField(
state = rememberTextFieldState(),
lineLimits = TextFieldLineLimits.SingleLine
)
// [END android_compose_state_text_3]
}
@Preview
@Composable
fun Multiline() {
Spacer(modifier = Modifier.height(15.dp))
// [START android_compose_state_text_4]
TextField(
state = rememberTextFieldState("Hello\nWorld\nHello\nWorld"),
lineLimits = TextFieldLineLimits.MultiLine(1, 4)
)
// [END android_compose_state_text_4]
}
@Composable
fun StyleWithBrush() {
// [START android_compose_state_text_5]
val brush = remember {
Brush.linearGradient(
colors = listOf(Color.Red, Color.Yellow, Color.Green, Color.Blue, Color.Magenta)
)
}
TextField(
state = rememberTextFieldState(), textStyle = TextStyle(brush = brush)
)
// [END android_compose_state_text_5]
}
@Composable
fun StateHoisting() {
// [START android_compose_state_text_6]
val usernameState = rememberTextFieldState()
TextField(
state = usernameState,
lineLimits = TextFieldLineLimits.SingleLine,
placeholder = { Text("Enter Username") }
)
// [END android_compose_state_text_6]
}
@Composable
fun TextFieldInitialState() {
// [START android_compose_state_text_7]
TextField(
state = rememberTextFieldState(initialText = "Username"),
lineLimits = TextFieldLineLimits.SingleLine,
)
// [END android_compose_state_text_7]
}
@Preview(showBackground = true)
@Composable
fun TextFieldBuffer() {
// [START android_compose_state_text_8]
val phoneNumberState = rememberTextFieldState("1234567890")
TextField(
state = phoneNumberState,
keyboardOptions = KeyboardOptions(
keyboardType = KeyboardType.Phone
),
inputTransformation = InputTransformation.maxLength(10).then {
if (!asCharSequence().isDigitsOnly()) {
revertAllChanges()
}
},
outputTransformation = OutputTransformation {
if (length > 0) insert(0, "(")
if (length > 4) insert(4, ")")
if (length > 8) insert(8, "-")
}
)
// [END android_compose_state_text_8]
}
@Preview
@Composable
fun EditTextFieldState() {
val usernameState = rememberTextFieldState("I love Android")
editTFState(usernameState)
}
fun editTFState(textFieldState: TextFieldState) {
// [START android_compose_state_text_9]
// Initial textFieldState text passed in is "I love Android"
// textFieldState.text : I love Android
// textFieldState.selection: TextRange(14, 14)
textFieldState.edit { insert(14, "!") }
// textFieldState.text : I love Android!
// textFieldState.selection: TextRange(15, 15)
textFieldState.edit { replace(7, 14, "Compose") }
// textFieldState.text : I love Compose!
// textFieldState.selection: TextRange(15, 15)
textFieldState.edit { append("!!!") }
// textFieldState.text : I love Compose!!!!
// textFieldState.selection: TextRange(18, 18)
textFieldState.edit { selectAll() }
// textFieldState.text : I love Compose!!!!
// textFieldState.selection: TextRange(0, 18)
// [END android_compose_state_text_9]
// [START android_compose_state_text_10]
textFieldState.setTextAndPlaceCursorAtEnd("I really love Android")
// textFieldState.text : I really love Android
// textFieldState.selection : TextRange(21, 21)
// [END android_compose_state_text_10]
// [START android_compose_state_text_11]
textFieldState.clearText()
// textFieldState.text :
// textFieldState.selection : TextRange(0, 0)
// [END android_compose_state_text_11]
}
class TextFieldViewModel : ViewModel() {
val usernameState = TextFieldState()
fun validateUsername() {
}
}
val textFieldViewModel = TextFieldViewModel()
@Composable
fun TextFieldKeyboardOptions() {
// [START android_compose_state_text_13]
TextField(
state = textFieldViewModel.usernameState,
keyboardOptions = KeyboardOptions(imeAction = ImeAction.Next),
onKeyboardAction = { performDefaultAction ->
textFieldViewModel.validateUsername()
performDefaultAction()
}
)
// [END android_compose_state_text_13]
}
@Composable
fun TextFieldInputTransformation() {
// [START android_compose_state_text_14]
TextField(
state = rememberTextFieldState(),
lineLimits = TextFieldLineLimits.SingleLine,
inputTransformation = InputTransformation.maxLength(10)
)
// [END android_compose_state_text_14]
}
// [START android_compose_state_text_15]
class CustomInputTransformation : InputTransformation {
override fun TextFieldBuffer.transformInput() {
}
}
// [END android_compose_state_text_15]
// [START android_compose_state_text_16]
class DigitOnlyInputTransformation : InputTransformation {
override fun TextFieldBuffer.transformInput() {
if (!asCharSequence().isDigitsOnly()) {
revertAllChanges()
}
}
}
// [END android_compose_state_text_16]
@Composable
fun ChainInputTransformation() {
// [START android_compose_state_text_17]
TextField(
state = rememberTextFieldState(),
inputTransformation = InputTransformation.maxLength(6)
.then(CustomInputTransformation()),
)
// [END android_compose_state_text_17]
}
// [START android_compose_state_text_18]
class CustomOutputTransformation : OutputTransformation {
override fun TextFieldBuffer.transformOutput() {
}
}
// [END android_compose_state_text_18]
// [START android_compose_state_text_19]
class PhoneNumberOutputTransformation : OutputTransformation {
override fun TextFieldBuffer.transformOutput() {
if (length > 0) insert(0, "(")
if (length > 4) insert(4, ")")
if (length > 8) insert(8, "-")
}
}
// [END android_compose_state_text_19]
@Composable
fun TextFieldOutputTransformation() {
// [START android_compose_state_text_20]
TextField(
state = rememberTextFieldState(),
outputTransformation = PhoneNumberOutputTransformation()
)
// [END android_compose_state_text_20]
}