4
4

More than 1 year has passed since last update.

Androidでボタンを一定時間無効にする方法

Last updated at Posted at 2022-06-16

概要

Androidのボタンの連打を防ぐためボタンが押されたら一定時間無効化したいことがあった。
そのため、ボタンを一定時間無効にする方法を記載する。
言語はKotlin

ボタンを一定時間無効にする方法

以下のような関数を定義する。

// Buttonを指定mill秒の間無効化する
fun disableButtonFixedTime(button: Button, millSec: Long) {
    button.isEnabled = false
    Handler().postDelayed({ button.isEnabled = true }, millSec)
}

ボタンを押したときに上記の関数を呼び出すことによって指定時間ボタンを無効化することができる。

button.setOnClickListener {
    disableButtonFixedTime(it, 1000)
    something() // ボタンを押したときにやりたい処理
}
4
4
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
4
4