You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Jetpack Compose自动填充框架:谷歌强密码无法回填问题

Google Autofill强密码建议无法回填至输入框的问题

问题概述

此前使用Google一键登录,现在尝试接入Autofill功能。在Compose中自定义了Autofill Modifier,当使用邮箱类型的Autofill时功能正常,但密码类型的强密码建议场景出现问题:谷歌密码管理器能正常唤起并生成强密码,但输入用户名点击保存后,密码并未回填至输入框。同时在传统XML布局的EditText中测试,也存在相同问题。

自定义Compose Autofill Modifier代码

@OptIn(ExperimentalComposeUiApi::class)
fun Modifier.autofill(
    autofillTypes: List<AutofillType>,
    onFill: ((String) -> Unit),
) = composed {
    val autofill = LocalAutofill.current
    val autofillNode = AutofillNode(onFill = onFill, autofillTypes = autofillTypes)
    LocalAutofillTree.current += autofillNode

    this
        .onGloballyPositioned {
            autofillNode.boundingBox = it.boundsInWindow()
        }
        .onFocusChanged { focusState ->
            autofill?.run {
                if (focusState.isFocused) {
                    requestAutofillForNode(autofillNode)
                } else {
                    cancelAutofillForNode(autofillNode)
                }
            }
        }
}

密码输入框使用代码

OutlinedTextField(
    keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Password),
    value = userUIState.password,
    onValueChange = { userUIState.onPasswordChanged(it) },
    trailingIcon = {
        val iconState =
            if (passwordVisible) Pair(R.drawable.visibility, "Hide password")
            else Pair(R.drawable.visibility_off, "Show password")

        IconButton(onClick = { passwordVisible = !passwordVisible }) {
            Icon(
                painter = painterResource(id = iconState.first),
                contentDescription = iconState.second
            )
        }
    },
    modifier = Modifier
        .fillMaxWidth()
        .autofill(
            autofillTypes = listOf(AutofillType.Password),
            onFill = { userUIState.onPasswordChanged(it) },
        ),
    visualTransformation = if (passwordVisible) VisualTransformation.None else PasswordVisualTransformation()
)

XML布局测试代码

<EditText
    android:id="@+id/passwordField"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:importantForAutofill="yes"
    android:autofillHints="password"
    android:inputType="textPassword"
    tools:ignore="MissingConstraints" />

问题细节

  • 仅强密码建议场景出现回填失败,普通密码自动填充功能正常
  • 开启开发者选项日志后,发现日志输出onAuthenticationResult(): id= 18743299, data=null,推测data为空是问题根源
  • 尝试实现AutofillCallback,但仅能获取事件ID,未能解决回填问题

内容的提问来源于stack exchange,提问作者Sarah Brenner

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 11:27:39