Wear OS 上的 Jetpack Compose 中的 BasicTextField 问题

Dmi*_*try 4 android wear-os android-jetpack-compose compose-wear

我是 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
Run Code Online (Sandbox Code Playgroud)

这是我在真实设备上得到的结果:

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
Run Code Online (Sandbox Code Playgroud)

这是我的“可组合”:

@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,
        )
    }
}
Run Code Online (Sandbox Code Playgroud)

Yur*_*mke 10

您应该避免在 Wear 上输入文本,但如果您确实需要它,GBoard 活动是激活的最佳方式。

\n

https://developer.android.com/reference/androidx/wear/input/RemoteInputIntentHelper.Companion#createActionRemoteInputIntent()

\n
@Composable\nfun TextInput() {\n  val label = remember { mutableStateOf("Start")}\n  val launcher =\n    rememberLauncherForActivityResult(ActivityResultContracts.StartActivityForResult()) {\n      it.data?.let { data ->\n        val results: Bundle = RemoteInput.getResultsFromIntent(data)\n        val ipAddress: CharSequence? = results.getCharSequence("ip_address")\n        label.value = ipAddress as String\n      }\n    }\n  Column() {\n    Spacer(modifier = Modifier.height(20.dp))\n    Chip(\n      label = { Text(label.value) },\n      onClick = {}\n    )\n    Chip(\n      label = { Text("Search with specific IP") },\n      onClick = {\n        val intent: Intent = RemoteInputIntentHelper.createActionRemoteInputIntent();\n        val remoteInputs: List<RemoteInput> = listOf(\n          RemoteInput.Builder("ip_address")\n            .setLabel("Manual IP Entry")\n            .wearableExtender {\n              setEmojisAllowed(false)\n              setInputActionType(EditorInfo.IME_ACTION_DONE)\n            }.build()\n        )\n\n        RemoteInputIntentHelper.putRemoteInputsExtra(intent, remoteInputs)\n\n        launcher.launch(intent)\n      }\n    )\n  }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

注意:正如@TiagoPeresFran\xc3\xa7a 在评论中提到的:

\n
\n

为了使用wearableExtender,您必须使用磨损输入依赖项的 1.2.0+ 版本。从今天开始,应该使用版本 1.2.0-alpha02。在您的应用程序 gradle 文件中,确保您有依赖项implementation \'androidx.wear:wear-input:1.2.0-alpha02\'

\n
\n

  • 您从哪里获得可穿戴扩展器? (3认同)