我有一个缩小视图大小的函数ACTION_DOWN
,并在上将其恢复为原始大小ACTION_UP
。此功能仅出于美观目的。我在按钮上设置了setOnClickListeners,以便它们(理论上...)可以在按下按钮时执行代码。
private fun scaleButton(theButton:View, grow:Boolean){
theButton.setOnTouchListener(View.OnTouchListener { v, event ->
if (event.action == MotionEvent.ACTION_DOWN) {
v.isPressed = true
if (grow) {
v.animate().scaleX(1.04F).scaleY(1.04F).setDuration(50)
} else {
v.animate().scaleX(0.97F).scaleY(0.97F).setDuration(50)
}
} else if (event.action == MotionEvent.ACTION_UP) {
v.isPressed = false
v.animate().scaleX(1.0F).scaleY(1.0F).setDuration(100)
}
false
})
}
问题setOnClickListener
永远不会被解决。
onCreate:
scaleButton(button1,false)
scaleButton(button2,true)
scaleButton(button3,false)
button1.setOnClickListener {
println("Button 1 Pressed")
}
button2.setOnClickListener {
println("Button 2 Pressed")
}
button3.setOnClickListener {
println("Button 3 Pressed")
}
如何同时通过动画(触摸)缩放按钮和触发 setOnClickListener
您的点击监听器永远不会触发,因为应该将触摸监听器告知系统何时点击了元素。你不是。可以在适当的时间添加performClick,或者将所有内容都放入触摸监听器中并在action_up上单击
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句