gpt4 book ai didi

android - 如何检测按钮上的连续多次点击 - 就像android开发者选项

转载 作者:行者123 更新时间:2023-12-04 12:51:52 25 4
gpt4 key购买 nike

我想实现与开发者选项的 android 设置应用程序类似的行为。我的意思是当用户连续点击一个按钮 7 次并且每次点击之间有一个小的延迟时,将出现一个弹出窗口要求他在应用程序中启用隐藏功能。

我怎样才能实现这种行为?

最佳答案

我是基于看门狗的概念开发的。如果 TimerWatchDog 的实例没有在指定的时间内刷新,它就会完成这项工作。 ContinuousClicksHandler 使用 TimerWatchDog 的实例并计算点击次数。

用法:

val clicksCount = 7
val maxInterval = 1000L

val continuousClicksHandler = ContinuousClicksHandler(clicksCount, maxInterval)

continuousClicksHandler.registerCallback(object : ContinuousClicksHandler.ContinuousClicksCallback {

override fun onContinuousClicksSuccessful() {
println("onContinuousClicksSuccessful!")
}

override fun onContinuousClicksFailed() {
println("onContinuousClicksFailed!")
}

})

button.setOnClickListener {
val count = continuousClicksHandler.click()
showToast("Needs ${clicksCount - count} more clicks to succeed!")
}

ContinuousClicksHandler.kt

/**
* @author aminography
*/
class ContinuousClicksHandler(
private val clicksCount: Int,
maxInterval: Long
) {

private var callback: ContinuousClicksCallback? = null
private val timerWatchDog = TimerWatchDog(maxInterval)
private var currentClicks = 0

fun click(): Int {
if (++currentClicks == clicksCount) {
timerWatchDog.cancel()
currentClicks = 0
callback?.onContinuousClicksSuccessful()
} else {
timerWatchDog.refresh {
currentClicks = 0
callback?.onContinuousClicksFailed()
}
}
return currentClicks
}

fun registerCallback(callback: ContinuousClicksCallback) {
this.callback = callback
}

interface ContinuousClicksCallback {

fun onContinuousClicksSuccessful()

fun onContinuousClicksFailed()
}

}

TimerWatchDog.kt

import java.util.*

/**
* @author aminography
*/
class TimerWatchDog(private val timeout: Long) {

private var timer: Timer? = null

fun refresh(job: () -> Unit) {
timer?.cancel()
timer = Timer()
timer?.schedule(object : TimerTask() {
override fun run() = job.invoke()
}, timeout)
}

fun cancel() = timer?.cancel()

}

关于android - 如何检测按钮上的连续多次点击 - 就像android开发者选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33596105/

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