Help us understand the problem. What is going on with this article?

View Listener to activate feature after N clicks within time limit

More than 1 year has passed since last update.

The following subclass of View.OnClickListener can be set as a listener to any View. When the user clicks on that view enough times within a defined time limit (here, the target is defined as 7 clicks within 2 seconds), the onCompletion handler is called.

This is a useful feature in some cases e.g. defining a double-click handler, activating a hidden debug mode while the app is running.

class MultipleClickListener :  View.OnClickListener {

    val maxDuration: Long = 2000 // in milliseconds
    val minTaps: Int = 7 // minimum number of clicks to complete
    var iconTapCount: Int = 0
    var start: Long = 0

    val now: Long
    get() = System.currentTimeMillis()

    override fun onClick(v: View?) {

        // This method returns a ViewPropertyAnimator object, which we could use to 
        // animate the View per click for visual feedback.
        val animator = v?.animate()

        if(0 == iconTapCount || now - start > maxDuration) {
            iconTapCount = 0
            start = now
        }
        if(now - start < maxDuration) {
            iconTapCount += 1
        }
        if(iconTapCount == minTaps) {
            onCompletion()
        }
    }

    // do something e.g. open a new view, switch on debug mode etc
    fun onCompletion() {}

}
cayozin
iOS/Android development (Swift/C++/Java/Kotlin/XML/Javascript), backend (php/mySQL) and Machine Learning (Python/Scala)
ayudante
いつもユーザー中心で技術者とコンサルタントがとことん考え抜く それがアユダンテです
https://ayudante.jp/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした