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

Jetpack Compose中OutlinedTextField键盘异常:无法自动弹出、无法输入但可隐藏的问题求助

Jetpack Compose中OutlinedTextField键盘异常:无法自动弹出、无法输入但可隐藏的问题求助

大家好,我最近在开发一个小费计算的Jetpack Compose应用,遇到了OutlinedTextField的键盘交互问题,折腾了好一阵都没搞定,来这里求助各位大佬!

先说说我的需求:我想做一个带美元图标的输入框,只允许输入数字;点击键盘的「下一步」按钮时,如果输入框有内容就隐藏键盘,没内容就让键盘保持显示。目前UI看起来是正常的,但核心的输入和键盘逻辑出了问题,具体异常表现如下:

  • 点击输入框后,键盘不会自动弹出,必须手动去唤起
  • 就算手动唤起了键盘,也完全没法在输入框里输入任何内容
  • 更奇怪的是,明明输入框看起来是空的,却能触发隐藏键盘的操作

下面是我的代码:

Components.kt

package com.example.jettipapp.components

import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.*
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.unit.sp

@Composable
fun InputField(
    modifier: Modifier = Modifier,
    valueState: MutableState<String>,
    labelId: String,
    enabled: Boolean,
    isSingleLine: Boolean,
    keyboardType: KeyboardType = KeyboardType.Number,
    imeAction: ImeAction = ImeAction.Next,
    onAction: KeyboardActions = KeyboardActions.Default
) {
    OutlinedTextField(
        value = TextFieldValue(annotatedString = buildAnnotatedString { valueState.value }),
        onValueChange = { currentValue: TextFieldValue ->
            valueState.value = currentValue.toString()
        },
        modifier = modifier,
        label = { Text(labelId) },
        leadingIcon = {
            Icon(
                imageVector = Icons.Rounded.AttachMoney,
                contentDescription = "Money Icon"
            )
        },
        singleLine = isSingleLine,
        textStyle = TextStyle(fontSize = 20.sp, color = MaterialTheme.colorScheme.onBackground),
        enabled = enabled,
        keyboardOptions = KeyboardOptions(keyboardType = keyboardType, imeAction = imeAction),
        keyboardActions = onAction,
        readOnly = false
    )
}

MainActivity.kt

package com.example.jettipapp

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.*
import com.example.jettipapp.ui.theme.JetTipAppTheme
import com.example.jettipapp.components.InputField

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContent {
            App {
                MainContent()
            }
        }
    }
}

@SuppressLint("UnusedMaterial3ScaffoldPaddingParameter")
@Composable
fun App(content: @Composable () -> Unit) {
    JetTipAppTheme {
        Scaffold(modifier = Modifier.fillMaxSize()) { paddingValues ->
            content()
        }
    }
}

@Composable
fun MainContent() {
    val totalBill = remember { mutableStateOf("") }
    val valid = remember(totalBill.value) {
        totalBill.value.trim().isNotEmpty()
    }
    val keyboardController = LocalSoftwareKeyboardController.current

    Surface(
        modifier = Modifier
            .padding(2.dp)
            .height(500.dp)
            .fillMaxWidth(),
        shape = RoundedCornerShape(10.dp),
        border = BorderStroke(2.dp, Color.LightGray)
    ) {
        Column() {
            InputField(
                valueState = totalBill,
                labelId = "Enter Bill:",
                enabled = true,
                isSingleLine = true,
                onAction = KeyboardActions {
                    if (!valid) return@KeyboardActions
                    keyboardController?.hide()
                }
            )
        }
    }
}

@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
    App {
        MainContent()
    }
}

我的开发环境是API 36(Android 16 "Baklava"),测试设备是Google Pixel 9a。有没有大佬能帮忙看看问题出在哪?感激不尽!

内容来源于stack exchange

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.07 07:32:58