最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

android - How to Implement a Button with Tap and Hold for IncrementDecrement Actions (+1 on Tap, +10 every 0.5s on Hold) in Jetp

programmeradmin1浏览0评论

I've been stuck on this for a while. I have two "buttons" that performs two actions: it adds/removes +-1 on a single click and adds/removes +-10 on a long click. I want to modify it so that it works like this:

When the user taps (clicks) the button, it adds/removes +-1. When the user taps and holds, it should add/remove +-10 every 0.5 seconds until the user releases the button.

How can i solve this in the best way? Best / F

Here is my current code:

Box(
    modifier = Modifier
        .fillMaxSize()
        .background(Color.Transparent)
        .weight(2f)
        binedClickable(
            onClick = {
                when (selectedCounter) {
                    "Life" -> if (life > 0) life -= 1
                    "Poison" -> if (poison > 0) poison -= 1
                }
                consecutiveTapsRemove = maxOf(0, consecutiveTapsRemove + 1)
            },
            onLongClick = {
                when (selectedCounter) {
                    "Life" -> if (life > 0) life -= 10
                    "Poison" -> if (poison > 0) poison -= 10
                }
                consecutiveTapsRemove = maxOf(0, consecutiveTapsRemove + 10)
            }
        ),
) {}

Box(
    modifier = Modifier
        .fillMaxSize()
        .background(Color.Transparent)
        .weight(2f)
        binedClickable(
            onClick = {
                when (selectedCounter) {
                    "Life" -> life += 1
                    "Poison" -> poison += 1
                }
                consecutiveTapsAdd = maxOf(0, consecutiveTapsAdd + 1)
            },
            onLongClick = {
                when (selectedCounter) {
                    "Life" -> life += 10
                    "Poison" -> poison += 10
                }
                consecutiveTapsAdd = maxOf(0, consecutiveTapsAdd + 10)
            }
        )
) {}

I've been stuck on this for a while. I have two "buttons" that performs two actions: it adds/removes +-1 on a single click and adds/removes +-10 on a long click. I want to modify it so that it works like this:

When the user taps (clicks) the button, it adds/removes +-1. When the user taps and holds, it should add/remove +-10 every 0.5 seconds until the user releases the button.

How can i solve this in the best way? Best / F

Here is my current code:

Box(
    modifier = Modifier
        .fillMaxSize()
        .background(Color.Transparent)
        .weight(2f)
        binedClickable(
            onClick = {
                when (selectedCounter) {
                    "Life" -> if (life > 0) life -= 1
                    "Poison" -> if (poison > 0) poison -= 1
                }
                consecutiveTapsRemove = maxOf(0, consecutiveTapsRemove + 1)
            },
            onLongClick = {
                when (selectedCounter) {
                    "Life" -> if (life > 0) life -= 10
                    "Poison" -> if (poison > 0) poison -= 10
                }
                consecutiveTapsRemove = maxOf(0, consecutiveTapsRemove + 10)
            }
        ),
) {}

Box(
    modifier = Modifier
        .fillMaxSize()
        .background(Color.Transparent)
        .weight(2f)
        binedClickable(
            onClick = {
                when (selectedCounter) {
                    "Life" -> life += 1
                    "Poison" -> poison += 1
                }
                consecutiveTapsAdd = maxOf(0, consecutiveTapsAdd + 1)
            },
            onLongClick = {
                when (selectedCounter) {
                    "Life" -> life += 10
                    "Poison" -> poison += 10
                }
                consecutiveTapsAdd = maxOf(0, consecutiveTapsAdd + 10)
            }
        )
) {}

Share Improve this question asked Feb 15 at 10:48 BjurhagerBjurhager 554 bronze badges 2
  • You should add a timer task that executes every 5 seconds. Meanwhile, if the user releases the button, cancel the task. – Rajen Raiyarela Commented Feb 15 at 11:21
  • this article may be helpfully. link: proandroiddev/… – Meet Commented Feb 15 at 15:12
Add a comment  | 

1 Answer 1

Reset to default 0

Solved it, Here is how i made it work.

var testAction by remember { mutableStateOf("none") }
var testCount by remember { mutableIntStateOf(0) }

Box(
    modifier = Modifier
        .padding(20.dp)
        .pointerInput(Unit) {
            detectTapGestures(
                onPress = {
                    val released = try {
                        tryAwaitRelease()
                    } catch (c: CancellationException) {
                        false
                    }
                    if (released) {
                        testAction = "Released"
                        // Stop Timer
                    } else {
                        testAction = "Canceled"
                    }
                },
                onTap = {
                    testCount += 1
                },
                onLongPress = {
                    testAction = "Long press"
                    // Start timer
                }
            )
        }
) {
    Column {
        Text(testCount.toString())  // Display the tap count
        Text(testAction)  // Display the action text
        Box(
            modifier = Modifier
                .background(Color.Red)
                .padding(20.dp)
        ) {
            Text("Hold Test")
        }
    }
}

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论