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

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 05:07:17