use*_*550 3 android android-jetpack android-jetpack-compose android-compose-button android-jetpack-compose-button
我找到了onClick
andonLongClick
和 Even的监听器onPress
,但是没有像buttonDown
and buttonUp
、 or onPress
and之类的事件/监听器onRelease
。
我错过了什么吗?我当前的用例是,当用户按下按钮时,我会增加计数,当用户释放按钮时,我会减少计数。但总的来说,我希望用户按下按钮后就开始发生一些事情,并在用户释放按钮时停止。(举个现实生活中的例子,看看 Facebook Messenger 如何录制视频,按住按钮开始录制,松开按钮后录制停止。
我在 Android 上使用 Jetpack Compose。
Gab*_*tti 11
您可以使用InteractionSource.collectIsPressedAsState
来了解按钮是否被按下。您可以添加副作用来了解按钮何时被释放。
就像是:
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")
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3876 次 |
最近记录: |