Sof*_*Guy 9 android android-jetpack-compose
我有一个带按钮的 Android 可组合用户界面。如何跟踪按钮长按事件?我让它为“文本”长按工作,但对于按钮它不起作用。如果我将修饰符应用于按钮,则如下所示,它不起作用。
Text(
text = view.text,
fontSize = view.textFontSize.toInt().sp,
fontWeight = FontWeight(view.textFontWeight.toInt()),
color = Color(android.graphics.Color.parseColor(view.textColor)),
modifier = Modifier.clickable(onClick = {
println("Single Click")
}, onLongClick = {
println("Long Click")
}, onDoubleClick = {
println("Double Tap")
})
)
Run Code Online (Sandbox Code Playgroud)
Pio*_*ski 92
combinedClickable您可以像下面这样使用:
Modifier
.combinedClickable(
onClick = { },
onLongClick = { },
)
Run Code Online (Sandbox Code Playgroud)
警告:使用 Compose,1.0.1此方法被标记为,@ExperimentalFoundationApi因此此答案可能会在未来版本中过时。
小智 29
也可以使用https://developer.android.com/jetpack/compose/gestures 。
例如:
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.foundation.gestures.detectTapGestures
modifier = Modifier
.weight(2f)
.pointerInput(Unit){
detectTapGestures(
onLongPress = {
// perform some action here..
}
)
}
Run Code Online (Sandbox Code Playgroud)
adn*_*eal 16
处理这个问题的最佳方法是自己推出Button。材质Button基本上就是 aSurface和 a Row。Modifier.clickable添加您自己的不起作用的原因是因为已经设置了。
因此,如果您想添加onLongPress等,您可以复制/粘贴默认实现并传入这些 lambda。
@Composable
@OptIn(ExperimentalMaterialApi::class, ExperimentalFoundationApi::class)
fun Button(
onClick: () -> Unit,
modifier: Modifier = Modifier,
onLongClick: (() -> Unit)? = null,
onDoubleClick: (() -> Unit)? = null,
enabled: Boolean = true,
interactionState: InteractionState = remember { InteractionState() },
elevation: ButtonElevation? = ButtonDefaults.elevation(),
shape: Shape = MaterialTheme.shapes.small,
border: BorderStroke? = null,
colors: ButtonColors = ButtonDefaults.buttonColors(),
contentPadding: PaddingValues = ButtonDefaults.ContentPadding,
content: @Composable RowScope.() -> Unit
) {
val contentColor by colors.contentColor(enabled)
Surface(
shape = shape,
color = colors.backgroundColor(enabled).value,
contentColor = contentColor.copy(alpha = 1f),
border = border,
elevation = elevation?.elevation(enabled, interactionState)?.value ?: 0.dp,
modifier = modifier.combinedClickable(
onClick = onClick,
onDoubleClick = onDoubleClick,
onLongClick = onLongClick,
enabled = enabled,
role = Role.Button,
interactionState = interactionState,
indication = null
)
) {
Providers(LocalContentAlpha provides contentColor.alpha) {
ProvideTextStyle(
value = MaterialTheme.typography.button
) {
Row(
Modifier
.defaultMinSizeConstraints(
minWidth = ButtonDefaults.MinWidth,
minHeight = ButtonDefaults.MinHeight
)
.indication(interactionState, rememberRipple())
.padding(contentPadding),
horizontalArrangement = Arrangement.Center,
verticalAlignment = Alignment.CenterVertically,
content = content
)
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
用法:
Button(
onClick = {},
onLongClick = {},
onDoubleClick = {}
) {
Text(text = "I'm a button")
}
Run Code Online (Sandbox Code Playgroud)
Meh*_*p73 11
根据文件
Modifier.pointerInput(Unit) {
detectTapGestures(
onPress = { /* Called when the gesture starts */ },
onDoubleTap = { /* Called on Double Tap */ },
onLongPress = { /* Called on Long Press */ },
onTap = { /* Called on Tap */ }
)
}
Run Code Online (Sandbox Code Playgroud)
由于某些原因,它不适用于Button,但您可以使用可Card组合项重新创建按钮并向其应用combinedClickable修饰符。
ElevatedCard(
modifier = modifier
.fillMaxHeight()
.padding(4.dp)
.clip(RoundedCornerShape(20.dp))
.combinedClickable(
interactionSource = remember {
MutableInteractionSource()
},
indication = rememberRipple(bounded = true),
onClick = onClick,
onLongClick = onLongClick)
) {
Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
content()
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2042 次 |
| 最近记录: |