Last active
September 17, 2019 14:53
-
-
Save hkusu/c24b491c33d75e19dc37b76705cae4a4 to your computer and use it in GitHub Desktop.
Androidの連続クリック対策
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Button | |
android:id="@+id/button2" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="ボタン" | |
android:onClick="@{(view) -> someViewModel.onButtonClicked(view)}" | |
/> | |
※ Data Binding でなくても普通にコードでリスナーをセットしてもOK(その場合でもViewは渡す) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun onButtonClicked(view: View?) { // unitテスト時はnullを渡す | |
view?.disableContinuousClicks() | |
// do something.. | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun View.disableContinuousClicks(interval: Long = 300) { | |
this.isClickable = false | |
this.postDelayed(interval) { | |
this.isClickable = true | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment