[英]How to detect button release after button press in Android Jetpack Compose?
I find listeners for onClick
and onLongClick
and even onPress
but there is no event/listener for something like buttonDown
and buttonUp
, or onPress
and onRelease
.我找到了
onClick
和onLongClick
甚至onPress
的监听器,但是没有事件/监听器用于buttonDown
和buttonUp
或onPress
和onRelease
之类的东西。
Am I missing something?我错过了什么吗? My current use case is that when a user presses a button I increment a count and when the user releases it I decrease the count.
我当前的用例是,当用户按下按钮时,我会增加计数,当用户释放按钮时,我会减少计数。 But in general I want something to start happening as soon as the user presses the button and stop when the user releases it.
但总的来说,我希望在用户按下按钮时立即开始发生某些事情,并在用户释放按钮时停止。 (For a real life example, see how Facebook Messenger records a video, you keep the button pressed to start and it stops when you release it.
(有关现实生活中的示例,请参阅 Facebook Messenger 如何录制视频,按住按钮开始,松开按钮时停止。
I am using Jetpack Compose on Android.我在 Android 上使用 Jetpack Compose。
You can use the InteractionSource.collectIsPressedAsState
to know if the Button is pressed.您可以使用
InteractionSource.collectIsPressedAsState
来了解按钮是否被按下。 You can add a side effect to know when the Button is released.您可以添加一个副作用来了解按钮何时被释放。
Something like:就像是:
val interactionSource = remember { MutableInteractionSource() }
val isPressed by interactionSource.collectIsPressedAsState()
var currentStateTxt by remember { mutableStateOf("Not Pressed") }
var currentCount by remember { mutableStateOf(0) }
if (isPressed){
//Pressed
currentStateTxt = "Pressed"
currentCount += 1
//Use if + DisposableEffect to wait for the press action is completed
DisposableEffect(Unit) {
onDispose {
//released
currentStateTxt = "Released"
}
}
}
Button(onClick={},
interactionSource = interactionSource
){
Text("Current state = $currentStateTxt")
Text("Count = $currentCount")
}
Use.pointerInput modifier:使用.pointerInput修饰符:
.pointerInput(Unit) {
forEachGesture {
awaitPointerEventScope {
awaitFirstDown()
//onPress actions here
do {
val event = awaitPointerEvent()
//Track other pointer evenst, like Drag etc...
} while (event.changes.any { it.pressed })
//onRelease actions here
}
}
}
these codes may be helpful for you这些代码可能对你有帮助
var isPressed by remember {
mutableStateOf(false)
}
.pointerInput(Unit) {
detectTapGestures(
onPress = {
try {
isPressed = true
isPlaying = true
sampleSong.start()
awaitRelease()
}
finally {
isPressed = false
isPlaying = false
sampleSong.pause()
}
I think you use Touch Listener on button, its easily detect button touch or untouch example我想你在按钮上使用 Touch Listener,它很容易检测到按钮触摸或未触摸的例子
override fun onTouchEvent(e: MotionEvent): Boolean {
val x: Float = e.x
val y: Float = e.y
when (e.action) {
MotionEvent.ACTION_MOVE -> {
var dx: Float = x - previousX
var dy: Float = y - previousY
// reverse direction of rotation above the mid-line
if (y > height / 2) {
dx *= -1
}
// reverse direction of rotation to left of the mid-line
if (x < width / 2) {
dy *= -1
}
renderer.angle += (dx + dy) * TOUCH_SCALE_FACTOR
requestRender()
}
}
previousX = x
previousY = y
return true
} }
More info of Touch Listener in this link Android Touch Listener Touch Listener 的更多信息在此链接Android Touch Listener
Modifier.pointerInput(Unit) {
detectTapGestures(
onPress = {
//start
val released = try {
tryAwaitRelease()
} catch (c: CancellationException) {
false
}
if (released) {
//ACTION_UP
} else {
//CANCELED
}
},
onTap = {
// onTap
},
onDoubleTap = {
//onDoubleTap
},
onLongPress = {
//onLongPress
}
)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.