Can't handle both click and touch events simultaneously

后端 未结 12 2065
时光说笑
时光说笑 2020-12-01 03:45

I am trying to handle touch events and click events on a button. I do the following:

button.setOnClickListener(clickListener);
button.setOnTouchListener(touc         


        
12条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-01 04:20

    All above answer is said that we can not handle both setOnTouchListener and setOnClickListener.
    However, I see we can handle both by return false in setOnTouchListener

    Example

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    
        button = findViewById(R.id.button)
        button.setOnClickListener {
            Log.i("TAG", "onClick")
        }
    
        button.setOnTouchListener { v, event ->
            Log.i("TAG", "onTouch " + event.action)
            false
        }
    }
    

    When I click at Button, logcat will display like

    I/TAG: onTouch 0
    I/TAG: onTouch 1
    I/TAG: onClick
    

提交回复
热议问题