Toggle Passwortfeld jetpack compose

Lesezeit: 3 Minuten

Benutzeravatar von clouddy
bewölkt

Hallo, ich versuche, visualTransformation dynamisch zu ändern, wenn der Benutzer auf die Schaltfläche „Kennwort anzeigen“ klickt. Ich kann das Passwort filtern, konnte es aber nicht im Klartext anzeigen. Irgendeine Idee dafür? Hier ist, was ich bisher bekommen habe.

fun UserInputText(
    keyboardType: KeyboardType = KeyboardType.Text,
    onTextChanged: (TextFieldValue) -> Unit,
    textFieldValue: TextFieldValue,
    visualTransformation: VisualTransformation = VisualTransformation.None,
    borderColor: Color = editTextBorderColor,
    keyboardShown: Boolean,
    onTextFieldFocused: (Boolean) -> Unit,
    focusState: Boolean,
    placeholder: String = "",
    modifier: Modifier = Modifier
) {
    Box(
        modifier = modifier.border(
            width = 2.dp,
            color = borderColor,
            shape = RoundedCornerShape(16.dp)
        )
    ) {
        var lastFocusState by remember { mutableStateOf(FocusState.Inactive) }
        val focusRequester = FocusRequester()
        val focusRequesterModifier = Modifier.focusRequester(focusRequester)

        BasicTextField(
            value = textFieldValue,
            onValueChange = { onTextChanged(it) },
            modifier =
            modifier.focus().then(focusRequesterModifier)
                .align(Alignment.TopStart)
                .focusObserver { state ->
                    if (lastFocusState != state) {
                        onTextFieldFocused(state == FocusState.Active)
                    }
                    lastFocusState = state
                },
            keyboardOptions = KeyboardOptions(
                keyboardType = keyboardType,
                imeAction = ImeAction.Send
            ),
            visualTransformation = visualTransformation,
            maxLines = 1,
            cursorColor = inputTextColor,
            textStyle = MaterialTheme.typography.body1.copy(color = inputTextColor)
        )
        if(keyboardType == KeyboardType.Password) {
            Image(
                vectorResource(id = R.drawable.ic_icons_watch_count_24), modifier = Modifier
                    .align(Alignment.TopEnd)
                    .padding(end = 16.dp, top = 16.dp).clickable(onClick = {})
            )
        }
    }
}

Benutzeravatar von Gabriele Mariotti
Gabriele Marotti

Sie können die Norm verwenden TextField zusammensetzbar:

var password by rememberSaveable { mutableStateOf("") }
var passwordVisible by rememberSaveable { mutableStateOf(false) }

TextField(
    value = password,
    onValueChange = { password = it },
    label = { Text("Password") },
    singleLine = true,
    placeholder = { Text("Password") },
    visualTransformation = if (passwordVisible) VisualTransformation.None else PasswordVisualTransformation(),
    keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Password),
    trailingIcon = {
        val image = if (passwordVisible)
            Icons.Filled.Visibility
        else Icons.Filled.VisibilityOff

        // Please provide localized description for accessibility services
        val description = if (passwordVisible) "Hide password" else "Show password"

        IconButton(onClick = {passwordVisible = !passwordVisible}){
            Icon(imageVector  = image, description)
        }
    }
)

Geben Sie hier die Bildbeschreibung ein

Hinweis: zu verwenden Icons.Filled.Visibility Und Icons.Filled.VisibilityOff füge die Abhängigkeiten hinzu: implementation "androidx.compose.material:material-icons-extended:$compose_version"

  • Gute Lösung. Eindrucksvoll!

    – Gary Chen

    25. August 2021 um 12:24 Uhr

  • Die Verwendung leerer Zeichenfolgen für Inhaltsbeschreibungen ist eine schlechte Angewohnheit. Es ist besser, sie auf null zu setzen, um anzuzeigen, dass sie rein dekorativ sind, oder in Ihrem Beispiel eine tatsächliche Zeichenfolge bereitzustellen, damit die Leute verstehen, warum sie vorhanden ist. Ich bin mir nicht sicher, wie ein Screenreader mit einem Compose TextField mit PasswordVisualTransform arbeiten würde, aber ich würde nicht darauf zählen, dass diese contentDescription unnötig ist.

    – omiwrench

    21. Dezember 2021 um 10:44 Uhr

  • @omiwrench Es ist nur ein Beispiel. Da die Antwort zeigt, wie ein Passwortfeld implementiert wird, ist es in jedem Fall richtig, auch die Beschreibung zu verwalten. Antwort aktualisiert. Danke für die Rückmeldung.

    – Gabriele Mariotti

    8. März 2022 um 11:22 Uhr

Überprüfen Sie dies:

    var passwordVisibility: Boolean by remember { mutableStateOf(false) }
    TextField(value = "Enter Password",
        visualTransformation = if (passwordVisibility) VisualTransformation.None else PasswordVisualTransformation(),
        leadingIcon = {
            IconButton(onClick = {
                passwordVisibility = !passwordVisibility
            }) {
                Icon(imageVector = vectorResource(id = R.drawable.ic_icons_watch_count_24))
            }
        },
        onValueChange = { })
    

Passwort in Jetpack Compose ausblenden/anzeigen

 @Composable
    fun CommonTextFieldPassword(
        text: MutableState<String>,
        placeholder: String,
        trailingIcon: Int = R.drawable.eye,
        visibility: MutableState<Boolean> = remember { mutableStateOf(false) }
    ) {
        TextField(
            value = text.value,
            onValueChange = { text.value = it },
            colors = TextFieldDefaults.textFieldColors(
                backgroundColor = Color.White,
                focusedLabelColor = fadedTextColor,
                textColor = headingColor,
                unfocusedLabelColor = fadedTextColor,
                unfocusedIndicatorColor = fadedTextColor,
                focusedIndicatorColor = headingColor
            ),
            label = { Text(text = placeholder) },
            trailingIcon = {
                Icon(
                    painter = painterResource(id = trailingIcon),
                    contentDescription = "",
                    modifier = Modifier
                        .size(25.dp)
                        .clickable {
                            visibility.value = !visibility.value
                        },
                    tint = if (visibility.value) titleColor else fadedTextColor
                )
            },
            modifier = Modifier.fillMaxWidth(),
            visualTransformation = if (visibility.value) VisualTransformation.None else PasswordVisualTransformation()
        )
    }

1443170cookie-checkToggle Passwortfeld jetpack compose

This website is using cookies to improve the user-friendliness. You agree by using the website further.

Privacy policy