0
Help us understand the problem. What are the problem?

More than 1 year has passed since last update.

posted at

updated at

ボタン連打を防ぐ方法 - kotlin

この記事は、Android #2 Advent Calender 2020 12日目の記事です。

できるもの

android-sample-sigle-tap.gif

やること

  1. タップ前の画面のレイアウトを追加
  2. タップ後の画面のレイアウトを追加
  3. extディレクトリに、ViewExt.ktを生成
  4. Activityから呼び出し

1. タップ前の画面のレイアウトを追加

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="204dp"
        android:text="TextView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

  1. タップ後の画面のレイアウトを追加
activity_next_page.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".NextPageActivity">

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="NextPage"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

2. extディレクトリに、ViewExt.ktを生成

ViewExt.ktを生成し、

スクリーンショット 2020-12-12 11.21.57.jpg

ボタン連打を防ぐための関数を、ViewExt.ktに拡張関数として定義

ViewExt.kt
private const val DELAY_TIME = 2000

fun View.setOnSingleClickListener(listener: () -> Unit) {
    var pushedAt = 0L
    val currentTime = System.currentTimeMillis()

    Log.d("TAP", "tap")

    if (currentTime - pushedAt < DELAY_TIME) return
    pushedAt = System.currentTimeMillis()
    listener()
}

4. Activityから呼び出し

MainActivity.kt
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val textview = findViewById<View>(R.id.textView) as TextView
        val button = findViewById<Button>(R.id.button) as Button

        textview.setOnSingleClickListener {
            startActivity(Intent(this, NextPageActivity::class.java))
        }
        button.setOnSingleClickListener {
            startActivity(Intent(this, NextPageActivity::class.java))
        }
    }
}

終了です

参考

ボタン連打されてしまい不正な動作をするのはよくある話
kotlinで定数を定義する時【const】vs【val】

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
Sign upLogin
0
Help us understand the problem. What are the problem?