📜  计算Android中视图上的点击次数(多次点击)

📅  最后修改于: 2021-05-10 16:58:32             🧑  作者: Mango

在本文中,统计了用户在短时间内在特定视图上执行的抽头数量,它可用于通过不同的抽头来添加不同的响应。例如,在单词上轻按一次将显示单词的含义,而在字典应用程序中双击则将显示其同义词。下面是一个示例,其中在textview中完成的点击次数显示为吐司。

第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()
                          
                          
                        // Handle Multiple Taps on the View////////////////////////////////
                       handler.postDelayed(Runnable {
                           
                         // Toast the number of Taps of current User Event
                                Toast.makeText(applicationContext, "$numberOfTaps Clicks", 
                                               Toast.LENGTH_SHORT)
                                        .show()
                            }, ViewConfiguration.getDoubleTapTimeout().toLong())
                     /////////////////////////////////////////////////////////////////////
                      
                    }
                }
                return true
            }
        })
    }
}


步骤3:在此步骤中,将onTouchListenter与视图一起添加。此处,在侦听器中将变量初始化为0,该变量将保持用户已完成的点击次数。每次用户在短时间内点击时,变量值都会增加一。下面是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()
                          
                          
                        // Handle Multiple Taps on the View////////////////////////////////
                       handler.postDelayed(Runnable {
                           
                         // Toast the number of Taps of current User Event
                                Toast.makeText(applicationContext, "$numberOfTaps Clicks", 
                                               Toast.LENGTH_SHORT)
                                        .show()
                            }, ViewConfiguration.getDoubleTapTimeout().toLong())
                     /////////////////////////////////////////////////////////////////////
                      
                    }
                }
                return true
            }
        })
    }
}

在仿真器上的输出:

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