android 如何隐藏键盘在对话框与撰写

2wnc66cl  于 2023-05-15  发布在  Android
关注(0)|答案(1)|浏览(148)

我使用BasicTextFieldDialog中构建我的视图,当我想通过ImeAction.Done关闭键盘时,但它不起作用。我试着用以下方法隐藏键盘:

val keyboardController = LocalSoftwareKeyboardController.current
keyboardController?.hide()

val focusManager = LocalFocusManager.current
focusManager.clearFocus()

但不管用有人有办法帮我解决这个案子吗?
我的代码:

@Composable
fun DialogEditNickNameView(){
    val name = remember { mutableStateOf("") }
    val keyboardController = LocalSoftwareKeyboardController.current
    

    Dialog(onDismissRequest = { }) {
        Column(
            modifier = Modifier
                .background(color = Color.White, shape = RoundedCornerShape(16.dp))
                .padding(vertical = 24.dp, horizontal = 16.dp),
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            Text(
                text = "Edit name",
                style = TextStyle(
                    fontFamily = robotoFamily,
                    fontWeight = FontWeight.Medium,
                    fontSize = 16.sp,
                    lineHeight = 24.sp,
                    color = colorResource(id = R.color.text_color)
                ),
            )

            Row(
                modifier = Modifier.padding(vertical = 16.dp)
            ) {
                BasicTextField(
                    value = name.value,
                    onValueChange = {
                        name.value = it
                    },
                    modifier = Modifier.fillMaxWidth(0.7f),
                    keyboardOptions = KeyboardOptions(
                        keyboardType = KeyboardType.Text,
                        imeAction = ImeAction.Done
                    ),
                    keyboardActions = KeyboardActions(onDone = {
                        keyboardController?.hide()
                    }),
                    singleLine = true,
                    textStyle = TextStyle(
                        fontFamily = robotoFamily,
                        fontWeight = FontWeight.Normal,
                        fontSize = 16.sp,
                        lineHeight = 24.sp,
                        color = colorResource(id = R.color.text_color)
                    ),
                )

                Spacer(modifier = Modifier.width(26.dp))

                Image(
                    painter = painterResource(id = R.drawable.ic_close_circle),
                    contentDescription = null,
                    modifier = Modifier.clickable { name.value = "" }
                )
            }
         }
}
mbzjlibv

mbzjlibv1#

Dialog有自己的LocalSoftwareKeyboardController
只需将代码移动到Dialog中:

Dialog(onDismissRequest = { }) {

    val keyboardController = LocalSoftwareKeyboardController.current

    //...Column

}

相关问题