短时间内检测到屏幕上的 5 次点击

问题描述 投票:0回答:0

我一直在尝试使用我从here复制并针对kotlin和setOnClickListener()修改它的代码来实现此代码以在3秒内检测屏幕上的5次点击。

private lateinit var binding: ActivityMainBinding
private var count = 0
private var startMillis: Long = 0

    @SuppressLint("ClickableViewAccessibility")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        binding.textView2.setOnClickListener {
            Toast.makeText(this, "touched1", Toast.LENGTH_SHORT) //This shows up

            //get system current milliseconds
            val time = System.currentTimeMillis()

            //if it is the first time, or if it has been more than 3 seconds since the first tap ( so it is like a new try), we reset everything
            if (startMillis == 0L || time - startMillis > 3000) {
                startMillis = time
                count = 1
            } else { //  time-startMillis< 3000
                count++
            }
            if (count == 5) {
                //Do things here
                Toast.makeText(this, "touched5", Toast.LENGTH_SHORT) //Doesn't show up
            }

        }

当我运行它时,出现了“touched1”,但它没有检测到多次点击。

android onclicklistener
© www.soinside.com 2019 - 2024. All rights reserved.