Enable double-click (Multi-click) events in Android

Source: Internet
Author: User

The first, easy-to-think approach. To implement a double-click, you need to save the time of the first click, use the variable, and then compare it with the time of the second click to see if the time interval is within the time you set (such as 500ms).
Long Firstclicktime = 0;long secondclicktime = 0;public void Click1 (view view) {if (Firstclicktime > 0) {secondclicktim E = Systemclock.uptimemillis (), if (Secondclicktime-firstclicktime <) {Toast.maketext (this, "first double-click Method", 0). Show () ;} Firstclicktime = 0;return;} Firstclicktime = Systemclock.uptimemillis (); New Thread (New Runnable () {@Overridepublic void run () {//try {thread.sleep ( Firstclicktime = 0;} catch (Interruptedexception e) {//TODO auto-generated catch Blocke.printstacktrace ();}}). Start ();}
Double-click events (multi-click events) have two important issues to consider: the time of the first click, the last (how to know the "last") Click Time, and after one click, wait for a period of time (such as 2s), double-click (Multi-click) how to ensure that the correct response. The above code solves both problems, but is inefficient and complex.  It is through a variable to store the time of the first click, by judging the size of the variable to determine whether it is a second click, solve the first problem; plus the sleep method of the child thread, if there is no second click in the 500ms, the variable is reset, which solves the second problem. However, this way to solve the double-click event is not bad, then three hit, four hit ... How did it work out?
Second, store variables in a different way
List<long> times = new arraylist<long> ();p ublic void Click2 (view view) {Times.add (systemclock.uptimemillis ()); if (times.size () = = 2) {if (Times.get (1)-times.get (0) <) {Times.clear (); Toast.maketext (This, "first double-click Method", 0). Show (); else {times.clear (); Times.add (Systemclock.uptimemillis ());}}}
In the second way, you use the list to hold the time of the click.  Now to analyze the implementation of the principle: 1, how to determine the second click?  By the length of the linked list, each time you click on the list length plus 1, when it is 2 o'clock, it means two clicks.  2. How to eliminate the impact of a time-lapse click event? If it is a normal double-click, click two times to complete a "reincarnation", the previous saved time data has been used, need to clear out, the specific operation is to empty the list. And if it is one click, the second click time is longer, the first click of the time is no use, the second click directly as a "first" click, specifically to empty the List,add second click event.
This method is much more efficient than the first method and is very easy to scale up to three strikes, four hits ... Event. Like what:
List<long> times = new arraylist<long> ();p ublic void Click2 (view view) {Times.add (systemclock.uptimemillis ()); if (times.size () = = 3) {if (Times.get (2)-times.get (0) <) {Times.clear (); Toast.maketext (This, "first double-click Method", 0). Show (); else {times.clear (); Times.add (Systemclock.uptimemillis ());}}}
What really needs to change is only two judging conditions!!

Third, the way Google programmers do it. Here is Google's three-click Method, I changed to write a double-click method
/** * Double-click event, multi-click event *///storage time array long[] mhits = new long[2];p ublic void DoubleClick () {//double-click event Response/** * arraycopy, copy array  * SRC to copy of source array * Srcpos source array start copy subscript position * DST target array * Dstpos begin to store the index position * length to be copied (number of elements) * *///to implement the shift operation of the array, click once, move left one bit, the end of the current boot time (CPU Time) System.arraycopy (mhits, 1, mhits, 0, mhits.length-1); mhits[mhits.length-1] = Systemclock.uptimemillis ();// Double-click event Interval 500msif (Mhits[0] >= (Systemclock.uptimemillis ()) {            //double-click after the specific operation//do}}
Very concise, the idea is similar, but the Google engineers use the array shift operation to eliminate the impact of the second problem. To implement a multi-click event, you only need to modify the array length.

Enable double-click (Multi-click) events in Android

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.