-
Notifications
You must be signed in to change notification settings - Fork 329
Update Autofill Snippets to include both state value based TF #763
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
795ab6b
d583ef3
496ad4b
e8fcca2
6cc1d8f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -40,24 +40,49 @@ import com.example.compose.snippets.touchinput.Button | |
|
|
||
| @Composable | ||
| fun AddAutofill() { | ||
| // [START android_compose_autofill_1] | ||
| var textFieldValue = remember { | ||
| mutableStateOf(TextFieldValue("")) | ||
| } | ||
|
|
||
| // [START android_compose_autofill_1_value] | ||
| TextField( | ||
| value = textFieldValue.value, | ||
| onValueChange = {textFieldValue.value = it}, | ||
MagicalMeghan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| modifier = Modifier.semantics { contentType = ContentType.Username } | ||
| ) | ||
| // [END android_compose_autofill_1_value] | ||
|
|
||
| // [START android_compose_autofill_1_state] | ||
| TextField( | ||
| state = rememberTextFieldState(), | ||
| modifier = Modifier.semantics { contentType = ContentType.Username } | ||
| ) | ||
| // [END android_compose_autofill_1] | ||
| // [END android_compose_autofill_1_state] | ||
| } | ||
|
|
||
| @Composable | ||
| fun AddMultipleTypesOfAutofill() { | ||
| // [START android_compose_autofill_2] | ||
| var textFieldValue = remember { | ||
MagicalMeghan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| mutableStateOf(TextFieldValue("")) | ||
| } | ||
| // [START android_compose_autofill_2_value] | ||
| TextField( | ||
| value = textFieldValue.value, | ||
| onValueChange = { textFieldValue.value = it }, | ||
| modifier = Modifier.semantics { | ||
| contentType = ContentType.Username + ContentType.EmailAddress | ||
| } | ||
| ) | ||
| // [END android_compose_autofill_2_value] | ||
|
|
||
| // [START android_compose_autofill_2_state] | ||
| TextField( | ||
| state = rememberTextFieldState(), | ||
| modifier = Modifier.semantics { | ||
| contentType = ContentType.Username + ContentType.EmailAddress | ||
| } | ||
| ) | ||
| // [END android_compose_autofill_2] | ||
| // [END android_compose_autofill_2_state] | ||
| } | ||
|
|
||
| @Composable | ||
|
|
@@ -68,11 +93,35 @@ fun AutofillManager() { | |
| } | ||
|
|
||
| @Composable | ||
| fun SaveDataWithAutofill() { | ||
| fun SaveDateWithAutofillValue() { | ||
| var textFieldValue = remember { | ||
| mutableStateOf(TextFieldValue("")) | ||
| } | ||
| // [START android_compose_autofill_4] | ||
| // [START android_compose_autofill_4_value] | ||
| val autofillManager = LocalAutofillManager.current | ||
|
|
||
| Column { | ||
| TextField( | ||
| value = textFieldValue.value, | ||
| onValueChange = { textFieldValue.value = it }, | ||
| modifier = Modifier.semantics { contentType = ContentType.NewUsername } | ||
| ) | ||
|
|
||
| Spacer(modifier = Modifier.height(16.dp)) | ||
|
|
||
| TextField( | ||
| value = textFieldValue.value, | ||
| onValueChange = { textFieldValue.value = it }, | ||
| modifier = Modifier.semantics { contentType = ContentType.NewPassword } | ||
| ) | ||
| } | ||
| // [END android_compose_autofill_4_value] | ||
| } | ||
|
||
|
|
||
| @Composable | ||
| fun SaveDataWithAutofillState() { | ||
|
|
||
| // [START android_compose_autofill_4_state] | ||
| val autofillManager = LocalAutofillManager.current | ||
|
|
||
| Column { | ||
|
|
@@ -88,12 +137,42 @@ fun SaveDataWithAutofill() { | |
| modifier = Modifier.semantics { contentType = ContentType.NewPassword } | ||
| ) | ||
| } | ||
| // [END android_compose_autofill_4] | ||
| // [END android_compose_autofill_4_state] | ||
| } | ||
|
|
||
|
|
||
| @Composable | ||
| fun SaveDataWithAutofillOnClick() { | ||
| // [START android_compose_autofill_5] | ||
| fun SaveDataWithAutofillOnClickValue() { | ||
| var textFieldValue = remember { | ||
| mutableStateOf(TextFieldValue("")) | ||
| } | ||
| // [START android_compose_autofill_5_value] | ||
| val autofillManager = LocalAutofillManager.current | ||
|
|
||
| Column { | ||
| TextField( | ||
| value = textFieldValue.value, | ||
| onValueChange = { textFieldValue.value = it }, | ||
| modifier = Modifier.semantics { contentType = ContentType.NewUsername }, | ||
| ) | ||
|
|
||
| Spacer(modifier = Modifier.height(16.dp)) | ||
|
|
||
| TextField( | ||
| value = textFieldValue.value, | ||
| onValueChange = { textFieldValue.value = it }, | ||
| modifier = Modifier.semantics { contentType = ContentType.NewPassword }, | ||
| ) | ||
|
|
||
| // Submit button | ||
| Button(onClick = { autofillManager?.commit() }) { Text("Reset credentials") } | ||
| } | ||
| // [END android_compose_autofill_5_value] | ||
| } | ||
|
Comment on lines
145
to
174
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This snippet has a couple of issues:
Here is a corrected version: @Composable
fun SaveDataWithAutofillOnClickValue() {
val usernameValue = remember {
mutableStateOf(TextFieldValue(""))
}
val passwordValue = remember {
mutableStateOf(TextFieldValue(""))
}
// [START android_compose_autofill_5_value]
val autofillManager = LocalAutofillManager.current
Column {
TextField(
value = usernameValue.value,
onValueChange = { usernameValue.value = it },
modifier = Modifier.semantics { contentType = ContentType.NewUsername },
)
Spacer(modifier = Modifier.height(16.dp))
TextField(
value = passwordValue.value,
onValueChange = { passwordValue.value = it },
modifier = Modifier.semantics { contentType = ContentType.NewPassword },
)
// Submit button
Button(onClick = { autofillManager?.commit() }) { Text("Reset credentials") }
}
// [END android_compose_autofill_5_value]
} |
||
|
|
||
| @Composable | ||
| fun SaveDataWithAutofillOnClickState() { | ||
| // [START android_compose_autofill_5_state] | ||
| val autofillManager = LocalAutofillManager.current | ||
|
|
||
| Column { | ||
|
|
@@ -112,12 +191,31 @@ fun SaveDataWithAutofillOnClick() { | |
| // Submit button | ||
| Button(onClick = { autofillManager?.commit() }) { Text("Reset credentials") } | ||
| } | ||
| // [END android_compose_autofill_5] | ||
| // [END android_compose_autofill_5_state] | ||
| } | ||
|
|
||
| @Composable | ||
| fun CustomAutofillHighlightValue() { | ||
| var textFieldValue = remember { | ||
MagicalMeghan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| mutableStateOf(TextFieldValue("")) | ||
| } | ||
|
|
||
| // [START android_compose_autofill_6_value] | ||
| val customHighlightColor = Color.Red | ||
|
|
||
| CompositionLocalProvider(LocalAutofillHighlightColor provides customHighlightColor) { | ||
| TextField( | ||
| value = textFieldValue.value, | ||
| onValueChange = { textFieldValue.value = it }, | ||
| modifier = Modifier.semantics { contentType = ContentType.Username } | ||
| ) | ||
| } | ||
| // [END android_compose_autofill_6_value] | ||
| } | ||
|
|
||
| @Composable | ||
| fun CustomAutofillHighlight(customHighlightColor: Color = Color.Red) { | ||
| // [START android_compose_autofill_6] | ||
| fun CustomAutofillHighlightState(customHighlightColor: Color = Color.Red) { | ||
| // [START android_compose_autofill_6_state] | ||
| val customHighlightColor = Color.Red | ||
|
|
||
| CompositionLocalProvider(LocalAutofillHighlightColor provides customHighlightColor) { | ||
|
|
@@ -126,5 +224,5 @@ fun CustomAutofillHighlight(customHighlightColor: Color = Color.Red) { | |
| modifier = Modifier.semantics { contentType = ContentType.Username } | ||
| ) | ||
| } | ||
| // [END android_compose_autofill_6] | ||
| // [END android_compose_autofill_6_state] | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.