Skip to content

Commit 105645a

Browse files
committed
Update AutofillSnippets.kt
Updated to Material3 TextField
1 parent 8bc39e3 commit 105645a

File tree

1 file changed

+48
-24
lines changed

1 file changed

+48
-24
lines changed

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

Lines changed: 48 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,39 +19,48 @@ package com.example.compose.snippets.text
1919
import androidx.compose.foundation.layout.Column
2020
import androidx.compose.foundation.layout.Spacer
2121
import androidx.compose.foundation.layout.height
22-
import androidx.compose.foundation.text.BasicTextField
2322
import androidx.compose.foundation.text.LocalAutofillHighlightColor
24-
import androidx.compose.foundation.text.input.TextFieldState
23+
import androidx.compose.material3.TextField
2524
import androidx.compose.material3.Text
2625
import androidx.compose.runtime.Composable
2726
import androidx.compose.runtime.CompositionLocalProvider
27+
import androidx.compose.runtime.mutableStateOf
2828
import androidx.compose.runtime.remember
2929
import androidx.compose.ui.Modifier
3030
import androidx.compose.ui.autofill.ContentType
3131
import androidx.compose.ui.graphics.Color
3232
import androidx.compose.ui.platform.LocalAutofillManager
3333
import androidx.compose.ui.semantics.contentType
3434
import androidx.compose.ui.semantics.semantics
35+
import androidx.compose.ui.text.input.TextFieldValue
3536
import androidx.compose.ui.unit.dp
3637
import com.example.compose.snippets.touchinput.Button
3738

3839

3940
@Composable
4041
fun AddAutofill() {
42+
var textFieldValue = remember {
43+
mutableStateOf(TextFieldValue(""))
44+
}
4145
// [START android_compose_autofill_1]
42-
BasicTextField(
43-
state = remember { TextFieldState() },
46+
TextField(
47+
value = textFieldValue.value,
48+
onValueChange = {textFieldValue.value = it},
4449
modifier = Modifier.semantics { contentType = ContentType.Username }
4550
)
4651
// [END android_compose_autofill_1]
4752
}
4853

4954
@Composable
5055
fun AddMultipleTypesOfAutofill() {
56+
var textFieldValue = remember {
57+
mutableStateOf(TextFieldValue(""))
58+
}
5159
// [START android_compose_autofill_2]
5260
Column {
53-
BasicTextField(
54-
state = remember { TextFieldState() },
61+
TextField(
62+
value = textFieldValue.value,
63+
onValueChange = {textFieldValue.value = it},
5564
modifier =
5665
Modifier.semantics {
5766
contentType = ContentType.Username + ContentType.EmailAddress
@@ -70,12 +79,16 @@ fun AutofillManager() {
7079

7180
@Composable
7281
fun SaveDataWithAutofill() {
82+
var textFieldValue = remember {
83+
mutableStateOf(TextFieldValue(""))
84+
}
7385
// [START android_compose_autofill_4]
7486
val autofillManager = LocalAutofillManager.current
7587

7688
Column {
77-
BasicTextField(
78-
state = remember { TextFieldState() },
89+
TextField(
90+
value = textFieldValue.value,
91+
onValueChange = {textFieldValue.value = it},
7992
modifier =
8093
Modifier.semantics {
8194
contentType = ContentType.NewUsername
@@ -84,8 +97,9 @@ fun SaveDataWithAutofill() {
8497

8598
Spacer(modifier = Modifier.height(16.dp))
8699

87-
BasicTextField(
88-
state = remember { TextFieldState() },
100+
TextField(
101+
value = textFieldValue.value,
102+
onValueChange = {textFieldValue.value = it},
89103
modifier =
90104
Modifier.semantics {
91105
contentType = ContentType.NewPassword
@@ -97,11 +111,16 @@ fun SaveDataWithAutofill() {
97111

98112
@Composable
99113
fun SaveDataWithAutofillOnClick() {
114+
var textFieldValue = remember {
115+
mutableStateOf(TextFieldValue(""))
116+
}
100117
// [START android_compose_autofill_5]
101118
val autofillManager = LocalAutofillManager.current
119+
102120
Column {
103-
BasicTextField(
104-
state = remember { TextFieldState() },
121+
TextField(
122+
value = textFieldValue.value,
123+
onValueChange = {textFieldValue.value = it},
105124
modifier =
106125
Modifier.semantics {
107126
contentType = ContentType.NewUsername
@@ -110,8 +129,9 @@ fun SaveDataWithAutofillOnClick() {
110129

111130
Spacer(modifier = Modifier.height(16.dp))
112131

113-
BasicTextField(
114-
state = remember { TextFieldState() },
132+
TextField(
133+
value = textFieldValue.value,
134+
onValueChange = {textFieldValue.value = it},
115135
modifier =
116136
Modifier.semantics {
117137
contentType = ContentType.NewPassword
@@ -126,18 +146,22 @@ fun SaveDataWithAutofillOnClick() {
126146

127147
// [START android_compose_autofill_6]
128148
@Composable
129-
fun customizeAutofillHighlight() {
149+
fun CustomAutofillHighlight(customHighlightColor: Color = Color.Red) {
150+
var textFieldValue = remember {
151+
mutableStateOf(TextFieldValue(""))
152+
}
153+
// [START android_compose_autofill_6]
130154
val customHighlightColor = Color.Red
131-
132155
CompositionLocalProvider(LocalAutofillHighlightColor provides customHighlightColor) {
133-
Column {
134-
BasicTextField(
135-
state = remember { TextFieldState() },
136-
modifier = Modifier.semantics {
137-
contentType = ContentType.Username
138-
}
139-
)
140-
}
156+
TextField(
157+
value = textFieldValue.value,
158+
onValueChange = {textFieldValue.value = it},
159+
modifier = Modifier.semantics {
160+
contentType = ContentType.Username
161+
}
162+
)
141163
}
164+
// [END android_compose_autofill_6]
142165
}
143166
// [END android_compose_autofill_6]
167+

0 commit comments

Comments
 (0)