Jetpack Compose中TextField从后台返回后显示错误键盘问题
问题:Jetpack Compose TextField在应用前后台切换后无法正确显示电话键盘
我正在将原生Android应用迁移至Jetpack Compose,当前在Fragment中使用ComposeView实现登录界面。界面内有一个仅接受手机号的OutlinedTextField:
- 应用首次启动时,TextField获焦会正常显示电话键盘
- 应用进入后台再回到前台后,点击TextField会弹出普通键盘
实现代码
OutlinedTextField( value = phoneNumber.value, onValueChange = onValueChange, textStyle = textStyle18Normal.copy( textDirection = TextDirection.Ltr ), label = { Text( text = hintText, ) }, keyboardOptions = KeyboardOptions( keyboardType = KeyboardType.Phone, ), isError = isError.value, supportingText = { if (isError.value) { Text( modifier = Modifier .fillMaxWidth() .testTag("inputError"), text = errorMessage, ) } }, shape = RoundedCornerShape(4.dp), colors = OutlinedTextFieldDefaults.colors( focusedBorderColor = LocalCustomColorsPalette.current.primary, unfocusedBorderColor = LocalCustomColorsPalette.current.neutral40, errorBorderColor = LocalCustomColorsPalette.current.semanticError, errorLabelColor = LocalCustomColorsPalette.current.semanticError, errorLeadingIconColor = LocalCustomColorsPalette.current.semanticError, errorCursorColor = LocalCustomColorsPalette.current.semanticError, errorSupportingTextColor = LocalCustomColorsPalette.current.semanticError, unfocusedLabelColor = LocalCustomColorsPalette.current.neutral60, ), modifier = modifier .focusRequester(focusRequester) .onFocusChanged { if (it.isFocused) { keyboardController?.show() } } )
已尝试的方案
- 已在
keyboardOptions中明确设置keyboardType = KeyboardType.Phone - 尝试将键盘类型放入
remember代码块中,问题依旧 - 替换为
KeyboardType.Number,表现相同,前后台切换后仍弹出普通键盘
现象对比

内容的提问来源于stack exchange,提问作者Hossein Panahi Irdemousa
相关产品推荐
相关产品推荐

