相关疑难解决方法(0)

android 编写文本字段如何在外部触摸时关闭键盘

这是一个在全屏列中包含 TextField 的行。onFocusChanged当点击文本字段(进入活动状态)以及按下键盘上的“完成”按钮时,将调用回调。但当我在 TextField 外部点击时,处理程序不会被调用。所以我无法关闭键盘。任何想法?

val (nameText, setNameText) = remember { mutableStateOf("")}
val keyboardController = LocalSoftwareKeyboardController.current
val focusRequester = FocusRequester()

Column(
    modifier = Modifier
        .fillMaxWidth()
        .fillMaxHeight()
        .padding(10.dp)
) { 
    Row(
        modifier = Modifier
            .fillMaxWidth(),
        horizontalArrangement = Arrangement.SpaceAround
    ) {
        TextField(
            value = nameText,
            onValueChange = setNameText,
            modifier = Modifier
                .focusRequester(focusRequester)
                .onFocusChanged {
                    if (!it.isFocused) {
                        keyboardController?.hide()
                    }
                }
                .padding(horizontal = 0.dp, vertical = 0.dp)
                .height(50.dp)
                .fillMaxWidth(),
            KeyboardOptions(
                keyboardType = KeyboardType.Text,
                imeAction = androidx.compose.ui.text.input.ImeAction.Done
            ),
            keyboardActions = KeyboardActions(onDone = …
Run Code Online (Sandbox Code Playgroud)

android android-jetpack-compose

16
推荐指数
1
解决办法
1万
查看次数

标签 统计

android ×1

android-jetpack-compose ×1