如何在Jetpack Compose中的自定义键盘中将字母大写?

huangapple go评论83阅读模式
英文:

How to capitalize letters in custom keyboard in Jatpack Compose?

问题

以下是您要翻译的内容:

"I've been trying to figure out how to capitalize letters in my custom IME keyboard. I created capitalize key but I don't know what I have to put into

clickable(interactionSource = interactionSource, indication = null)

Maybe someone knows more how works with keyboard in Compose. Do you guys know how to achieve this in Jetpack Compose?"

英文:

I've been trying to figure out how to capitalize letters in my custom IME keyboard. I created capitalize key but I don't know what I have to put into

clickable(interactionSource = interactionSource, indication = null)

Maybe someone knows more how works with keyboard in Compose. Do you guys know how to achieve this in Jetpack Compose?

@Composable
fun KeyboardKeyCaps(
    keyboardKey: String, viewKeyboard: KeyboardViewModel
) {
    val interactionSource = remember { MutableInteractionSource() }
    val pressed = interactionSource.collectIsPressedAsState()
    val context = LocalContext.current
    val viewmodel = viewKeyboard
    val color by viewmodel.colorKeys.collectAsState()
    Box(contentAlignment = Alignment.Center) {
        Text(keyboardKey,
            Modifier
                .background(Color(android.graphics.Color.parseColor("#" + color)))
                .border(3.dp, Color.Black)
                .clickable(interactionSource = interactionSource, indication = null) {
                    (context as IMEService).currentInputConnection.let {

                    }
                }
                .padding(
                    start = 12.dp, end = 12.dp, top = 16.dp, bottom = 16.dp
                )

        )
        if (pressed.value) {
            Text(
                keyboardKey,
                Modifier
                    .border(1.dp, Color.Black)
                    .background(Color.Gray)
                    .padding(
                        start = 13.dp, end = 13.dp, top = 17.dp, bottom = 17.dp
                    )
            )
        }
    }
}

Usual Key

@Composable
fun KeyboardKey(
    keyboardKey: String, viewKeyboard: KeyboardViewModel
) {
    val interactionSource = remember { MutableInteractionSource() }
    val pressed = interactionSource.collectIsPressedAsState()
    val context = LocalContext.current
    val viewmodel = viewKeyboard
    val color by viewmodel.colorKeys.collectAsState()
    Box(contentAlignment = Alignment.Center) {
        Text(keyboardKey,
            Modifier
                .background(Color(android.graphics.Color.parseColor("#" + color)))
                .border(3.dp, Color.Black)
                .clickable(interactionSource = interactionSource, indication = null) {
                    (context as IMEService).currentInputConnection.commitText(
                        keyboardKey, 0
                    )
                }
                .padding(
                    start = 12.dp, end = 12.dp, top = 16.dp, bottom = 16.dp
                )

        )
        if (pressed.value) {
            Text(
                keyboardKey,
                Modifier.graphicsLayer(clip = false)
                    .border(1.dp, Color.Black)
                    .background(Color.Gray)
                    .padding(
                        start = 16.dp, end = 16.dp, top = 36.dp, bottom = 16.dp
                    )
            )
        }
    }
}

Screenshots

What I try to do

What I have done

答案1

得分: 0

https://github.com/IBRUTALI/KeyboardApp
这是如何在Compose中将按键大写的最佳示例。

英文:

https://github.com/IBRUTALI/KeyboardApp
There is the best example How to capitalize key in compose.

huangapple
  • 本文由 发表于 2023年6月29日 04:57:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76576657.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定