Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
342 views
in Technique[技术] by (71.8m points)

android - Can't handle both click and touch events simultaneously

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

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

When any one listener is registered things work fine but when I try to use them both only the touch events are fired. Any workaround? What am I doing wrong?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Its a little tricky.

If you set onTouchListener you need to return true in ACTION_DOWN, to tell the system that I have consumed the event and it won't trickle down to other listeners.

But then OnClickListener won't be fired.

So you might think, I will just do my thing there and return false so I can receive clicks too. If you do so, it will work, but you won't be subscribed to other upcoming touch events (ACTION_MOVE, ACTION_UP) Therefore, the only option is to return true there, but then you won't receive any click events as we said previously.

So you need to perform the click manually in the ACTION_UP with view.performClick()

This will work.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...