1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Android】ボタンのダブルタップを検知する方法とHandlerについて少し【kotlin】

Posted at

はじめに

今回はボタンのダブルタップ(ダブルクリック)について記事にします。
AndroidではGestureDetectorを使用することで、タッチイベント系のさまざまな処理を検知できるようですが、今回は(そんなに沢山使いたい訳じゃないし、一つのボタンだけのために・・・)と思って別のやり方にしました。

ダブルクリック処理

var buttonClickCount = 0

binding.button.setOnClickListener {
        myLocationButtonClickCount++
        Handler(Looper.getMainLooper()).postDelayed({

            if (buttonClickCount == 1) {
                // シングルクリック時の処理
            } else if (buttonClickCount == 2) {
                // ダブルクリック時の処理
            }
                myLocationButtonClickCount = 0
            }, 300)

Handler(Looper.getMainLooper()).postDelayed({

Handlerの引数にLooperを指定します。
Looperは、UI を管理するスレッドのメインスレッドをバインドします。”Looper.getMainLooper()”

HandlerのpostDelayedを使用することで、遅延を発生させて、その遅延中に行いたい処理を第一引数に。
第二引数に遅延させたい時間をミリ秒で指定します。
 "300"を入れると 0.3秒間に何回クリックされたかを検知できます。

参考

↓Handlerクラスについて

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?