使用 Jetpack Compose 构建软件键盘 - 使用 Jetpack Compose 的 IME 输入法

Yan*_*ick 7 android android-jetpack-compose

在 Jetpack Compose 中构建一个简单的键盘相当简单明了。

编辑:工作示例:https ://github.com/THEAccess/compose-keyboard-ime

我用这个构建了一个非常简单的 KeyRow:

密钥.kt

@Composable
fun Key(modifier: Modifier = Modifier, label: String, onClick: () -> Unit) {
    val shape = RoundedCornerShape(4.dp)
    //TODO: make clickable outside but don't show ripple
    Box(modifier = modifier
            .padding(2.dp)
            .clip(shape)
            .clickable(onClick = onClick)
            .background(Color.White)
            .padding(vertical = 12.dp, horizontal = 4.dp), contentAlignment = Alignment.Center) {
        Text(text = label, fontSize = 20.sp)
    }
}
Run Code Online (Sandbox Code Playgroud)

关键行.kt

@Composable
fun KeyRow(keys: List<String>) {
    Row(modifier = Modifier.fillMaxWidth().background(color = grey200)) {
        keys.forEach {
            Key(modifier = Modifier.weight(1f), label = it, onClick = {  })
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

看起来就是这样:

2

我想实现这个动画:

3

然而,我目前坚持这个

![4]

层次结构

-Keyboard
--KeyRow
---KeyLayout
----Key
----KeyPressedOverlay (only visible when pressed)
Run Code Online (Sandbox Code Playgroud)

我的主要问题是我不知道如何在不使父布局变大的情况下显示 KeyPressedOverlay Composale (大于 Key Composable) 。因此,我需要以某种方式溢出父布局。

ngl*_*ber 6

不确定这是否是最好的方法(可能不是),但我找到了一个使用ConstraintLayout...的解决方案

val keys = listOf("A", "B", "C", "D")
ConstraintLayout(
    modifier = Modifier.graphicsLayer(clip = false)
) {
    val refs = keys.map { createRef() }
    refs.forEachIndexed { index, ref ->
        val modifier = when (index) {
            0 -> Modifier.constrainAs(ref) {
                start.linkTo(parent.start)
            }
            refs.lastIndex -> Modifier.constrainAs(ref) {
                start.linkTo(refs[index - 1].end)
                end.linkTo(parent.end)
            }
            else -> Modifier.constrainAs(ref) {
                start.linkTo(refs[index - 1].end)
                end.linkTo(refs[index + 1].start)
            }
        }
        val modifierPressed = Modifier.constrainAs(createRef()) {
            start.linkTo(ref.start)
            end.linkTo(ref.end)
            bottom.linkTo(ref.bottom)
        }
        KeyboardKey(
            keyboardKey = keys[index],
            modifier = modifier,
            modifierPressed = modifierPressed,
            pressed = { s -> /* Do something with the key */}
        )
    }
}
Run Code Online (Sandbox Code Playgroud)

这里的一个重要细节是graphicLayer(clip = false)(与 View Toolkit 中的类似clipChildren)。然后,我为每个键和按下的键创建一个修饰符。请注意,modifierPressed与另一个修改器的中心/底部对齐。最后KeyboardKey描述如下。

@Composable
fun KeyboardKey(
    keyboardKey: String,
    modifier: Modifier,
    modifierPressed: Modifier,
    pressed: (String) -> Unit
) {
    var isKeyPressed by remember { mutableStateOf(false) }
    Text(keyboardKey, Modifier
        .then(modifier)
        .pointerInput(Unit) {
            detectTapGestures(onPress = {
                isKeyPressed = true
                val success = tryAwaitRelease()
                if (success) {
                    isKeyPressed = false
                    pressed(keyboardKey)
                } else {
                    isKeyPressed = false
                }
            })
        }
        .background(Color.White)
        .padding(
            start = 12.dp,
            end = 12.dp,
            top = 16.dp,
            bottom = 16.dp
        ),
        color = Color.Black
    )
    if (isKeyPressed) {
        Text(
            keyboardKey, Modifier
                .then(modifierPressed)
                .background(Color.White)
                .padding(
                    start = 16.dp,
                    end = 16.dp,
                    top = 16.dp,
                    bottom = 48.dp
                ),
            color = Color.Black
        )
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我得到的结果: 键盘效果

编辑: 添加更多逻辑,我能够得到这个...... 在此输入图像描述

我希望这次有帮助;)这是要点,以防万一... https://gist.github.com/nglauber/4cb1573efba9024c008ea71f3320b4d8