The ontouch and onclick events of the android floating box coexist.

Source: Internet
Author: User

In the Android app, I made a floating box and set the layout interface to an imageview. setontouchlistener is used to move the image, and setonclicklistener is the event triggered by clicking the image, I didn't think much at the beginning and found that only one event can be triggered. After checking the information, I learned that when the ontouch event returns true, the event will not be passed to the onclick event (the current event has been processed and does not need to be passed down ), therefore, we need to control when the ontouch event returns true or false.
To enable the ontouch and onclick events to coexist.

Now I will paste the key code for analysis:

Ismove is a global variable used to record whether the finger moves on the screen of the mobile phone. False indicates no, and true indicates sliding. This flag is critical.

Next, let's see how the ontouchlistener interface is implemented.

 

As mentioned above, you need to control when the ontouch event returns true or false to enable the event to coexist with the ontouch event. Therefore, when the finger is pressed, I move ismove = false. At this time, the finger does not

Move on the screen. If your finger leaves the screen, ismove = false is returned directly. When the ontouch event returns false, the event is passed to the onclick event. The click event is valid.

If the finger moves, simove = true. When the ontouch event returns true, the event is not transmitted to the onclick event. At this time, only the imageview is moving on the screen.

We also hope that you will pay attention to setting the properties of window when using code to implement a floating box:

There are a lot of online demos for the implementation of floating boxes. I will not elaborate on them.

Then we only need to bind the imageview to the listener.

The following is the code implementation of my entire demo for your reference. If you have any questions, please come up with me.

Http://download.csdn.net/detail/xiana2012/4889310

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.