Wear OS上Jetpack Compose中的BasicTextfield问题 [英] Problem with BasicTextField in Jetpack Compose on Wear OS

查看:49
本文介绍了Wear OS上Jetpack Compose中的BasicTextfield问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新手,在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

这是我的"Composable":

@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,
        )
    }
}

推荐答案

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

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

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

        RemoteInputIntentHelper.putRemoteInputsExtra(intent, remoteInputs)

        launcher.launch(intent)
      }
    )
  }
}

这篇关于Wear OS上Jetpack Compose中的BasicTextfield问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆