📜  轻按两下Android中的按钮

📅  最后修改于: 2021-05-10 15:50:18             🧑  作者: Mango

检测对Button的双击,即每当用户双击任何Button时,如何检测到它,并根据Button可以添加与之对应的响应。此处显示了一个示例,其中检测到按钮上的双击,并以吐司的形式添加了与之对应的响应。

第1步:在Android Studio中创建一个空活动。要创建一个项目,请遵循本文-如何在Android Studio中创建/启动新项目? 。检查所选的主要语言是否为Kotlin。

步骤2:在activity_main.xml中,添加一个将检测双击的按钮。

XML


  
  
    


Kotlin
package org.geeksforgeeks.doubletap_onbutton
  
import android.os.Bundle
import android.view.View
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
  
  
class MainActivity : AppCompatActivity() {
  
  
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
  
        // Declare the button from the layout file as Text View
        // Since the method works only on Views
        val dBtn = findViewById(R.id.btn)
  
          
        // Implementing a DoubleClickListener on the Button
        dBtn.setOnClickListener(object : DoubleClickListener() {
            override fun onDoubleClick(v: View?) {
                Toast.makeText(applicationContext,"Double Click",Toast.LENGTH_SHORT).show()
            }
        })
    }
  
    // This class has methods that check if two clicks were registered
    // within a span of DOUBLE_CLICK_TIME_DELTA i.e., in our case
    // equivalent to 300 ms
    abstract class DoubleClickListener : View.OnClickListener {
        var lastClickTime: Long = 0
        override fun onClick(v: View?) {
            val clickTime = System.currentTimeMillis()
            if (clickTime - lastClickTime < DOUBLE_CLICK_TIME_DELTA) {
                onDoubleClick(v)
            }
            lastClickTime = clickTime
        }
  
        abstract fun onDoubleClick(v: View?)
  
        companion object {
            private const val DOUBLE_CLICK_TIME_DELTA: Long = 300 //milliseconds
        }
    }
}


步骤3:在此步骤中,为双击添加抽象类,并设置将使用抽象类的onClickListener。并表演烤面包。下面是MainActivity.kt类的代码。

科特林

package org.geeksforgeeks.doubletap_onbutton
  
import android.os.Bundle
import android.view.View
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
  
  
class MainActivity : AppCompatActivity() {
  
  
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
  
        // Declare the button from the layout file as Text View
        // Since the method works only on Views
        val dBtn = findViewById(R.id.btn)
  
          
        // Implementing a DoubleClickListener on the Button
        dBtn.setOnClickListener(object : DoubleClickListener() {
            override fun onDoubleClick(v: View?) {
                Toast.makeText(applicationContext,"Double Click",Toast.LENGTH_SHORT).show()
            }
        })
    }
  
    // This class has methods that check if two clicks were registered
    // within a span of DOUBLE_CLICK_TIME_DELTA i.e., in our case
    // equivalent to 300 ms
    abstract class DoubleClickListener : View.OnClickListener {
        var lastClickTime: Long = 0
        override fun onClick(v: View?) {
            val clickTime = System.currentTimeMillis()
            if (clickTime - lastClickTime < DOUBLE_CLICK_TIME_DELTA) {
                onDoubleClick(v)
            }
            lastClickTime = clickTime
        }
  
        abstract fun onDoubleClick(v: View?)
  
        companion object {
            private const val DOUBLE_CLICK_TIME_DELTA: Long = 300 //milliseconds
        }
    }
}

在仿真器上的输出:

想要一个节奏更快,更具竞争性的环境来学习Android的基础知识吗?
单击此处前往由我们的专家精心策划的指南,以使您立即做好行业准备!