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

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

Last updated at Posted at 2020-12-12

この記事は、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】

1
0
1

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?