如何使用JetPack组合Android以编程为TextField的Keypress事件?

发布于 2025-01-26 19:55:22 字数 694 浏览 6 评论 0原文

当用户输入TextField时,它将调用OnValueChange {}回调。但是,当我们在TextField中设置值时,onValueChange {}回调将无法调用。

我发现 https://issuetracker.google.com/issues/issues/172239032 开放问题。

以下是我定义了Textfield的代码段。

OutlinedTextField(
        value = enteredValues[index],
        onValueChange = { value: String ->
            onTextFieldValueChange(value)
        },
        singleLine = true,
        keyboardOptions = keyboardOption,
        colors = textFieldColors,
    )

要获取OnValueChange的回调,我想以编程方式调用按键事件,所以我可能会得到OnValueChange回调。谁能给我解决方案?如何在JetPack组合中以编程方式使用按键?

When user type in TextField it will call onValueChange{} callback. but when we set the value in TextField the onValueChange{} callback will not call.

I found https://issuetracker.google.com/issues/172239032 open issue.

Below is a code snippet where I have defined TextField.

OutlinedTextField(
        value = enteredValues[index],
        onValueChange = { value: String ->
            onTextFieldValueChange(value)
        },
        singleLine = true,
        keyboardOptions = keyboardOption,
        colors = textFieldColors,
    )

To get the callback of onValueChange I want to call the keypress event programmatically so might be I will get onValueChange callback. Can anyone give me a solution? how to use keypress programmatically in jetpack compose?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我不知道您为什么要这样做,但是这是一个建议/解决方法...

您可以模拟以下的关键事件(这个答案)

fun simulateKeyPress(context: Context, key: Int) {
    val a = context as Activity
    a.window.decorView.rootView
    val inputConnection = BaseInputConnection(
        a.window.decorView.rootView,
        true
    )
    val downEvent = KeyEvent(KeyEvent.ACTION_DOWN, key)
    val upEvent = KeyEvent(KeyEvent.ACTION_UP, key)
    inputConnection.sendKeyEvent(downEvent)
    inputConnection.sendKeyEvent(upEvent)
}

然后,您可以调用此功能传递要发送的关键代码...

@Composable
fun SimulateKeySender() {
    var text by remember {
        mutableStateOf("")
    }
    val context = LocalContext.current
    val focusRequester = remember {
        FocusRequester()
    }
    Column {
        Button(
            onClick = { simulateKeyPress(context, KeyEvent.KEYCODE_A) }
        ) {
            Text(text = "Send A")
        }
        TextField(
            value = text,
            onValueChange = { text = it },
            modifier = Modifier.focusRequester(focusRequester)
        )
    }
    LaunchedEffect(Unit) {
        focusRequester.requestFocus()
    }
}

注意仅在textfield具有焦点时才有效,这就是为什么我正在使用FocusRequester

I have no idea why you want to do this, but here it is a suggestion/workaround...

You can simulate the key event like below (credit for this answer):

fun simulateKeyPress(context: Context, key: Int) {
    val a = context as Activity
    a.window.decorView.rootView
    val inputConnection = BaseInputConnection(
        a.window.decorView.rootView,
        true
    )
    val downEvent = KeyEvent(KeyEvent.ACTION_DOWN, key)
    val upEvent = KeyEvent(KeyEvent.ACTION_UP, key)
    inputConnection.sendKeyEvent(downEvent)
    inputConnection.sendKeyEvent(upEvent)
}

Then, you can call this function passing the key code you want to send...

@Composable
fun SimulateKeySender() {
    var text by remember {
        mutableStateOf("")
    }
    val context = LocalContext.current
    val focusRequester = remember {
        FocusRequester()
    }
    Column {
        Button(
            onClick = { simulateKeyPress(context, KeyEvent.KEYCODE_A) }
        ) {
            Text(text = "Send A")
        }
        TextField(
            value = text,
            onValueChange = { text = it },
            modifier = Modifier.focusRequester(focusRequester)
        )
    }
    LaunchedEffect(Unit) {
        focusRequester.requestFocus()
    }
}

Notice that will only work if the TextField has focus, this is why I'm using the FocusRequester.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文