Skip to content

Commit 1cefa75

Browse files
committed
Migrate to use BasicSecureTextField.
1 parent 718d8b3 commit 1cefa75

File tree

1 file changed

+19
-13
lines changed
  • compose/snippets/src/main/java/com/example/compose/snippets/text

1 file changed

+19
-13
lines changed

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

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,12 @@ import androidx.compose.foundation.layout.Row
3232
import androidx.compose.foundation.layout.fillMaxWidth
3333
import androidx.compose.foundation.layout.padding
3434
import androidx.compose.foundation.layout.width
35+
import androidx.compose.foundation.shape.RoundedCornerShape
36+
import androidx.compose.foundation.text.BasicSecureTextField
3537
import androidx.compose.foundation.text.KeyboardOptions
38+
import androidx.compose.foundation.text.input.TextFieldDecorator
39+
import androidx.compose.foundation.text.input.TextFieldState
40+
import androidx.compose.foundation.text.input.TextObfuscationMode
3641
import androidx.compose.foundation.text.selection.DisableSelection
3742
import androidx.compose.foundation.text.selection.SelectionContainer
3843
import androidx.compose.material.icons.Icons
@@ -43,6 +48,7 @@ import androidx.compose.material3.LocalTextStyle
4348
import androidx.compose.material3.MaterialTheme
4449
import androidx.compose.material3.OutlinedTextField
4550
import androidx.compose.material3.Text
51+
import androidx.compose.material3.TextButton
4652
import androidx.compose.material3.TextField
4753
import androidx.compose.runtime.Composable
4854
import androidx.compose.runtime.derivedStateOf
@@ -861,24 +867,24 @@ val LightBlue = Color(0xFF0066FF)
861867
val Purple = Color(0xFF800080)
862868

863869
// [START android_compose_text_auto_format_phone_number_showhidepassword]
870+
@Preview
864871
@Composable
865872
fun PasswordTextField() {
866-
var password by rememberSaveable { mutableStateOf("") }
873+
val state = remember { TextFieldState() }
867874
var showPassword by remember { mutableStateOf(false) }
868-
val passwordVisualTransformation = remember { PasswordVisualTransformation() }
869-
870-
TextField(
871-
value = password,
872-
onValueChange = { password = it },
873-
label = { Text("Enter password") },
874-
visualTransformation = if (showPassword) {
875-
VisualTransformation.None
875+
BasicSecureTextField(
876+
state = state,
877+
textObfuscationMode =
878+
if (showPassword) {
879+
TextObfuscationMode.Visible
876880
} else {
877-
passwordVisualTransformation
881+
TextObfuscationMode.RevealLastTyped
878882
},
879-
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Password),
880-
modifier = Modifier.fillMaxWidth(),
881-
trailingIcon = {
883+
modifier = Modifier.fillMaxWidth()
884+
.padding(6.dp)
885+
.border(1.dp, Color.LightGray, RoundedCornerShape(6.dp))
886+
.padding(6.dp),
887+
decorator = {
882888
Icon(
883889
if (showPassword) {
884890
Icons.Filled.Visibility

0 commit comments

Comments
 (0)