📜  在Android中使用onTouchListener对象在视图上轻按三次

📅  最后修改于: 2021-05-13 15:41:24             🧑  作者: Mango

为了检测三次轻击,即,每当用户在任何视图上三次轻击时,如何检测到它,并且根据该视图,可以添加与之对应的响应。这里显示了一个示例,其中用户在视图上三次轻击,将其检测到并以烤面包的形式添加与视图响应相对应的内容。

第1步:在Android Studio中创建一个空活动。要创建一个,请遵循以下文章-https://www.geeksforgeeks.org/android-how-to-create-start-a-new-project-in-android-studio/。检查所选的主要语言是否为Kotlin。

步骤2:在activity_main.xml中未做任何更改。由于已经存在一个textview,因此将三击的响应添加到其中。

XML


  
    
  


Kotlin
package org.geeksforgeeks.tripletap
  
import android.os.Bundle
import android.os.Handler
import android.view.MotionEvent
import android.view.View
import android.view.ViewConfiguration
import android.widget.Button
import android.widget.RelativeLayout
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)
  
        // Declaring Text View from the Layout file
        val tvv = findViewById(R.id.tv)
  
        // Implementing onTouchListener on our Text View
        tvv.setOnTouchListener(object : View.OnTouchListener {
          // Handler to handle the number of clicks
            var handler: Handler = Handler()
            var numberOfTaps = 0
            var lastTapTimeMs: Long = 0
            var touchDownMs: Long = 0
            override fun onTouch(v: View?, event: MotionEvent): Boolean {
                when (event.action) {
                    MotionEvent.ACTION_DOWN -> touchDownMs = System.currentTimeMillis()
                    MotionEvent.ACTION_UP -> {
                      // Handle the numberOfTaps
                        handler.removeCallbacksAndMessages(null)
                        if (System.currentTimeMillis() - touchDownMs 
                            > ViewConfiguration.getTapTimeout()) {
                            //it was not a tap
                            numberOfTaps = 0
                            lastTapTimeMs = 0
                        }
                        if (numberOfTaps > 0
                            && System.currentTimeMillis() - lastTapTimeMs 
                            < ViewConfiguration.getDoubleTapTimeout()
                        ) {
                          // if the view was clicked once
                            numberOfTaps += 1
                        } else {
                          // if the view was never clicked
                            numberOfTaps = 1
                        }
                        lastTapTimeMs = System.currentTimeMillis()
                          
                        // Triple Tap
                        if (numberOfTaps == 3) {
                            Toast.makeText(applicationContext, "Triple-Click", 
                                           Toast.LENGTH_SHORT).show()
                            // Double tap
                        } else if (numberOfTaps == 2) {
                            handler.postDelayed(Runnable {
                                Toast.makeText(applicationContext, "Double-Click", 
                                               Toast.LENGTH_SHORT)
                                    .show()
                            }, ViewConfiguration.getDoubleTapTimeout().toLong())
                        }
                    }
                }
                return true
            }
        })
    }
}


步骤3:在此步骤中,将onTouchListenter与视图一起添加。在侦听器中,这里将变量初始化为1,该变量将保持用户已完成的点击次数。如果在短时间内的分接头计数变为3,则将启动与视图相对应的响应。下面是MainActivity.kt类的代码。

科特林

package org.geeksforgeeks.tripletap
  
import android.os.Bundle
import android.os.Handler
import android.view.MotionEvent
import android.view.View
import android.view.ViewConfiguration
import android.widget.Button
import android.widget.RelativeLayout
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)
  
        // Declaring Text View from the Layout file
        val tvv = findViewById(R.id.tv)
  
        // Implementing onTouchListener on our Text View
        tvv.setOnTouchListener(object : View.OnTouchListener {
          // Handler to handle the number of clicks
            var handler: Handler = Handler()
            var numberOfTaps = 0
            var lastTapTimeMs: Long = 0
            var touchDownMs: Long = 0
            override fun onTouch(v: View?, event: MotionEvent): Boolean {
                when (event.action) {
                    MotionEvent.ACTION_DOWN -> touchDownMs = System.currentTimeMillis()
                    MotionEvent.ACTION_UP -> {
                      // Handle the numberOfTaps
                        handler.removeCallbacksAndMessages(null)
                        if (System.currentTimeMillis() - touchDownMs 
                            > ViewConfiguration.getTapTimeout()) {
                            //it was not a tap
                            numberOfTaps = 0
                            lastTapTimeMs = 0
                        }
                        if (numberOfTaps > 0
                            && System.currentTimeMillis() - lastTapTimeMs 
                            < ViewConfiguration.getDoubleTapTimeout()
                        ) {
                          // if the view was clicked once
                            numberOfTaps += 1
                        } else {
                          // if the view was never clicked
                            numberOfTaps = 1
                        }
                        lastTapTimeMs = System.currentTimeMillis()
                          
                        // Triple Tap
                        if (numberOfTaps == 3) {
                            Toast.makeText(applicationContext, "Triple-Click", 
                                           Toast.LENGTH_SHORT).show()
                            // Double tap
                        } else if (numberOfTaps == 2) {
                            handler.postDelayed(Runnable {
                                Toast.makeText(applicationContext, "Double-Click", 
                                               Toast.LENGTH_SHORT)
                                    .show()
                            }, ViewConfiguration.getDoubleTapTimeout().toLong())
                        }
                    }
                }
                return true
            }
        })
    }
}

输出

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