Continuously increase integer value as the button is pressed

后端 未结 9 1288
长情又很酷
长情又很酷 2020-12-04 11:10

I\'m new to Android so sorry if the question is easy to answer. I have two buttons, a decrease and an increase button, and in the middle of them a TextView which displays a

9条回答
  •  时光说笑
    2020-12-04 11:26

    for kotlin user

        myButton.setOnLongClickListener {
            val handler = Handler(Looper.myLooper()!!)
            val runnable : Runnable = object : Runnable {
                val number = 0
                override fun run() {
                    handler.removeCallbacks(this)
                    if (myButton.isPressed) {
                        val newNumber= number + 1
                        textView.text = "$newNumber Items"
                        handler.postDelayed(this, 100)
                    }
                }
            }
            handler.postDelayed(runnable,0)
            true
        }
    

提交回复
热议问题