gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 21:08:08 31 4
gpt4 key购买 nike

在 Jetpack Compose 中构建一个简单的键盘相当简单明了。
我使用这个构建了一个非常简单的 KeyRow:
key .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)
}
}
key 行.kt
@Composable
fun KeyRow(keys: List<String>) {
Row(modifier = Modifier.fillMaxWidth().background(color = grey200)) {
keys.forEach {
Key(modifier = Modifier.weight(1f), label = it, onClick = { })
}
}
}
这就是它的样子:
2
我想实现这个动画:
3
但是,我目前坚持这个
![4]
层次结构
-Keyboard
--KeyRow
---KeyLayout
----Key
----KeyPressedOverlay (only visible when pressed)
我的主要问题是我不知道如何显示 KeyPressedOverlay Composale(大于 Key Composable) 不使父布局变大 .结果,我需要以某种方式溢出父布局。

最佳答案

不确定这是否是最好的方法(可能不是),但我找到了使用 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 */}
)
}
}
这里的一个重要细节是 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
)
}
}
这是我得到的结果:
Keyboard effect
编辑:
添加更多逻辑,我能够得到这个......
enter image description here
我希望这次能有所帮助;)
这是要点以防万一...
https://gist.github.com/nglauber/4cb1573efba9024c008ea71f3320b4d8

关于android - 使用 Jetpack Compose 构建软件键盘 - 使用 Jetpack Compose 的 IME 输入法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65570024/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com