我使用BasicTextField
在Dialog
中构建我的视图,当我想通过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 = "" }
)
}
}
}
1条答案
按热度按时间mbzjlibv1#
Dialog
有自己的LocalSoftwareKeyboardController
。只需将代码移动到
Dialog
中: