0
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 3 years have passed since last update.

ボタン連打防止用コード

Posted at

自分用メモ
・ボタン連打防止用コード
※参考にする場合は、ご自身で検証してください。(検証不十分なため)

qiita.java

public class TestActivity {

  // !!!自分用メモでテスト不十分なことをご承知ください!!!

  // バックキー連打防止用フラグ
  private boolean backKeyEnabled = true;

  @Override
  public boolean dispatchKeyEvent(KeyEvent event) {
    // 連打しようとしたらボタン無効
    if(!backKeyEnabled) {
      return false;
    }
    // ボタンを押したら
    backKeyEnabled = false;
    // ボタンをしばらく押せないようにする
    new Handler().postDelayed(new Runnable() {
      @Override
      public void run() {
        backKeyEnabled = true; // 1sec後に押せるようにする
      }
    }, 1000);

    // 以後処理 
  }
}
0
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
0
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?