Kotlin OnTouchListener调用,但不会覆盖performClick

如何覆盖Kotlin中的performClick以避免警告。

next.setOnTouchListener(View.OnTouchListener { view, motionEvent -> when (motionEvent.action){ MotionEvent.ACTION_DOWN -> { val icon: Drawable = ContextCompat.getDrawable(activity.applicationContext, R.drawable.layer_bt_next) icon.setColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY) next.setImageDrawable(icon) } MotionEvent.ACTION_UP -> { //view.performClick() next.setImageResource(R.drawable.layer_bt_next) } } return@OnTouchListener true }) 

view.performClick不起作用。

好的,我通过重写OnTouch侦听器来解决自己的问题。

 override fun onTouch(view: View, motionEvent: MotionEvent): Boolean { when (view) { next -> { Log.d("next", "yeyy") when (motionEvent.action){ MotionEvent.ACTION_DOWN -> { val icon: Drawable = ContextCompat.getDrawable(activity.applicationContext, R.drawable.layer_bt_next) icon.setColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY) next.setImageDrawable(icon) } MotionEvent.ACTION_UP -> { view.performClick() next.setImageResource(R.drawable.layer_bt_next) } } } previous -> { //ingredients here XD } } return true } 

这样,我可以调用单个onTouch并将其实现为多个按钮,也可以使用onClick:

 view.performClick() 

不要忘记实施:

 View.OnTouchListener 

并设置监听器:

 next.setOnTouchListener(this) previous.setOnTouchListener(this) 

感谢上帝! 🙂