Talking about the problems triggered by the Android onclick and Onlongclick events

Source: Internet
Author: User

Before doing the button click event has not noticed some details, today made a button need to have a click and long click Trigger different effects, directly let activity implements Onclicklistener, Onlongclicklistener then adds the corresponding handler function.

    @Override    publicvoid  OnClick (View v) {        //  TODO auto-generated method Stub            }    @Override    publicboolean  Onlongclick (View v) {        //  TODO auto-generated method stub        return false ;    }

Without much consideration, I added the implementation I wanted directly within the function. In a casual test, I found that in my button click time there is a threshold, when I single-button time equal to this threshold, click events and long click events will simultaneously trigger. Click events naturally there is no problem, cause this problem must be a long click event out of the question, back to look closely at the method of long click event, found that it and click events basically no difference, the only difference is that there is a return value. So what's the use of this return value, check out the development documentation and describe the method as follows:

Public MethodsPublic Abstract Boolean Onlongclick (View v)Added in API Level 1

Called when a view has been clicked and held.

Parameters
V The view is clicked and held.
Returns
    • True if the callback consumed the long click, False otherwise.

This indicates that if the return value is true, the Click event will be exclusive by a long click, otherwise the opposite.

This gives you an idea of why a previous click triggered two click events.

The

Set the return value of Onlongclick to true to prevent this problem from occurring.

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.