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
相关产品推荐
相关产品推荐

