-
Notifications
You must be signed in to change notification settings - Fork 195
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
2 changed files
with
123 additions
and
1 deletion.
There are no files selected for viewing
122 changes: 122 additions & 0 deletions
122
compose/snippets/src/main/java/com/example/compose/snippets/text/AutofillSnippets.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
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.text.BasicTextField | ||
import androidx.compose.foundation.text.LocalAutofillHighlightColor | ||
import androidx.compose.foundation.text.input.TextFieldState | ||
import androidx.compose.material3.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.CompositionLocalProvider | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.autofill.ContentType | ||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.platform.LocalAutofillManager | ||
import androidx.compose.ui.semantics.contentType | ||
import androidx.compose.ui.semantics.semantics | ||
import androidx.compose.ui.unit.dp | ||
import com.example.compose.snippets.touchinput.Button | ||
|
||
@Composable | ||
fun AddAutofill() { | ||
// [START android_compose_autofill_1] | ||
BasicTextField( | ||
state = remember { TextFieldState("Enter your username.") }, | ||
modifier = Modifier.semantics { contentType = ContentType.Username } | ||
) | ||
// [END android_compose_autofill_1] | ||
} | ||
|
||
@Composable | ||
fun AddMultipleTypesOfAutofill() { | ||
// [START android_compose_autofill_2] | ||
Column { | ||
BasicTextField( | ||
state = remember { TextFieldState() }, | ||
modifier = | ||
Modifier.semantics { | ||
contentType = ContentType.Username + ContentType.EmailAddress | ||
}, | ||
) | ||
} | ||
// [END android_compose_autofill_2] | ||
} | ||
|
||
@Composable | ||
fun SaveDataWithAutofill() { | ||
// [START android_compose_autofill_3] | ||
// [START android_compose_autofill_4] | ||
val autofillManager = LocalAutofillManager.current | ||
// [END android_compose_autofill_3] | ||
|
||
Column { | ||
BasicTextField( | ||
state = remember { TextFieldState() }, | ||
modifier = | ||
Modifier.semantics { | ||
contentType = ContentType.NewUsername | ||
}, | ||
) | ||
|
||
Spacer(modifier = Modifier.height(16.dp)) | ||
|
||
BasicTextField( | ||
state = remember { TextFieldState() }, | ||
modifier = | ||
Modifier.semantics { | ||
contentType = ContentType.NewPassword | ||
}, | ||
) | ||
} | ||
// [END android_compose_autofill_4] | ||
} | ||
|
||
@Composable | ||
fun SaveDataWithAutofillOnClick() { | ||
// [START android_compose_autofill_5] | ||
val autofillManager = LocalAutofillManager.current | ||
Column { | ||
BasicTextField( | ||
state = remember { TextFieldState() }, | ||
modifier = | ||
Modifier.semantics { | ||
contentType = ContentType.NewUsername | ||
}, | ||
) | ||
|
||
Spacer(modifier = Modifier.height(16.dp)) | ||
|
||
BasicTextField( | ||
state = remember { TextFieldState() }, | ||
modifier = | ||
Modifier.semantics { | ||
contentType = ContentType.NewPassword | ||
}, | ||
) | ||
|
||
// Submit button | ||
Button(onClick = { autofillManager?.commit() }) { Text("Reset credentials") } | ||
} | ||
// [END android_compose_autofill_5] | ||
} | ||
|
||
// [START android_compose_autofill_6] | ||
@Composable | ||
fun customizeAutofillHighlight() { | ||
val customHighlightColor = Color.Red | ||
val usernameState = remember { TextFieldState() } | ||
|
||
CompositionLocalProvider(LocalAutofillHighlightColor provides customHighlightColor) { | ||
Column { | ||
BasicTextField( | ||
state = usernameState, | ||
modifier = Modifier.semantics { | ||
contentType = ContentType.Username | ||
} | ||
) | ||
} | ||
} | ||
} | ||
// [END android_compose_autofill_6] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters