我刚开始使用Compose,我在Wear OS上遇到了输入文本字段的问题。问题是我无法像在Android上一样使用软键盘。当我尝试在XML中实现相同的布局时,它却可以工作。
因此,当我点击输入文本字段时,键盘弹出然后隐藏。再次点击时,键盘弹出并保持打开状态,但如果我尝试输入任何内容,则输入字段中没有任何内容显示(在键盘本身上),尽管已输入的文本传递到了UI上的输入文本字段中。
以下是我在模拟器日志中点击输入文本字段以打开键盘时获得的内容:
以下是我在模拟器日志中点击输入文本字段以打开键盘时获得的内容:
2021-11-24 09:44:36.569 W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
2021-11-24 09:44:36.571 W/IInputConnectionWrapper: getTextAfterCursor on inactive InputConnection
2021-11-24 09:44:36.649 W/RecordingIC: requestCursorUpdates is not supported
这是我在真实设备上获得的结果:
2021-11-24 09:35:39.783 W/IInputConnectionWrapper: getExtractedText on inactive InputConnection
2021-11-24 09:35:39.872 W/IInputConnectionWrapper: beginBatchEdit on inactive InputConnection
2021-11-24 09:35:39.873 W/IInputConnectionWrapper: endBatchEdit on inactive InputConnection
2021-11-24 09:35:39.873 W/IInputConnectionWrapper: setComposingRegion on inactive InputConnection
2021-11-24 09:35:39.873 W/IInputConnectionWrapper: beginBatchEdit on inactive InputConnection
2021-11-24 09:35:39.873 W/IInputConnectionWrapper: endBatchEdit on inactive InputConnection
2021-11-24 09:35:39.873 W/IInputConnectionWrapper: beginBatchEdit on inactive InputConnection
2021-11-24 09:35:39.873 W/IInputConnectionWrapper: endBatchEdit on inactive InputConnection
2021-11-24 09:35:39.873 W/IInputConnectionWrapper: getExtractedText on inactive InputConnection
2021-11-24 09:35:39.882 W/IInputConnectionWrapper: beginBatchEdit on inactive InputConnection
2021-11-24 09:35:39.883 W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
2021-11-24 09:35:39.884 W/IInputConnectionWrapper: getTextAfterCursor on inactive InputConnection
2021-11-24 09:35:39.888 W/IInputConnectionWrapper: getSelectedText on inactive InputConnection
2021-11-24 09:35:39.890 W/IInputConnectionWrapper: endBatchEdit on inactive InputConnection
2021-11-24 09:35:39.891 W/IInputConnectionWrapper: beginBatchEdit on inactive InputConnection
2021-11-24 09:35:39.891 W/IInputConnectionWrapper: endBatchEdit on inactive InputConnection
2021-11-24 09:35:39.891 W/IInputConnectionWrapper: beginBatchEdit on inactive InputConnection
2021-11-24 09:35:39.891 W/IInputConnectionWrapper: endBatchEdit on inactive InputConnection
这是我的“可组合”代码:
@OptIn(ExperimentalComposeUiApi::class)
@Composable
fun ActivationScreen() {
var key by remember { mutableStateOf("") }
var isReady by remember {
mutableStateOf(false)
}
Column(modifier = Modifier
.padding(40.dp)
.fillMaxSize()
) {
val keyboardController = LocalSoftwareKeyboardController.current
val focusRequester = FocusRequester()
BasicTextField(
value = key,
onValueChange = {
//isReady = it.length>11
key = it
},
singleLine = true,
keyboardOptions = KeyboardOptions.Default.copy(
imeAction = ImeAction.Done
),
keyboardActions = KeyboardActions(
onDone = {
keyboardController?.hide()
}
),
modifier = Modifier
.size(140.dp, 20.dp)
.background(Color.White)
.align(Alignment.CenterHorizontally)
//.focusRequester(focusRequester)
//.focusOrder(focusRequester)
)
Text(
text = "ACTIVATION",
)
val status = if (isReady) "READY" else "NOT READY"
Text(
text = status,
)
}
}