Double-click (Multi-click) event in Android and double-click in android

Source: Internet
Author: User

Double-click (Multi-click) event in Android and double-click in android
First, easy to think. To achieve double-click, you need to save the time when you click for the first time. You need to use the variable, and then compare it with the time when you click for the second time, check whether the time interval is within your set time (for example, 500 ms ).

Long firstClickTime = 0; long secondClickTime = 0; public void click1 (View view) {if (firstClickTime> 0) {secondClickTime = SystemClock. uptimeMillis (); if (secondClickTime-firstClickTime <500) {Toast. makeText (this, "first double-click mode", 0 ). show () ;}firstclicktime = 0; return;} firstClickTime = SystemClock. uptimeMillis (); new Thread (new Runnable () {@ Overridepublic void run () {// try {Thread. sleep (500); firstClickTime = 0;} catch (InterruptedException e) {// TODO Auto-generated catch blocke. printStackTrace ();}}}). start ();}
Double-click events (Multi-click events) have two important issues that need to be considered: the time of the first click, and the time of the last click (how to know the last click); once clicked, wait for a period of time (such as 2 s), double-click again (Multi-click) to ensure correct response. The above code solves these two problems, but it is not efficient and complicated. It stores the time of the first click of a variable and determines whether the second click is the size of the variable to solve the first problem. In addition, the sleep method of the sub-thread is added, if there is no second click within ms, the variable will be reset to solve the second problem. However, this solution makes a combination of double-click events, so three-click, four-click, and so on ...... How can this problem be solved?
Second, storage of variables in another way
<Pre name = "code" class = "java"> List <Long> times = new ArrayList <Long> (); public void click2 (View view) {times. add (SystemClock. uptimeMillis (); if (times. size () = 2) {// you have already double-clicked it. The list can clear if (times. get (times. size ()-1)-times. get (0) <500) {times. clear (); Toast. makeText (this, "second double-click mode", 0 ). show ();} else {// In this case, the first click time is no longer useful, and the second is the "first" times. remove (0 );}}}


In the second method, use List to store the click Time. Now let's analyze the principle of this implementation: 1. How can we determine the second click? Through the length of the linked list, the length of each click list is increased by 1. If it is 2, it indicates that you have clicked twice. 2. How can I eliminate the impact of click events for a period of time? If it is a normal double-click, click twice to complete a "Cycle". The previously saved time data has been used up and needs to be cleared. The specific operation is to clear the List. However, if the second click takes a long time, and the first click is no longer useful, the second click will be regarded as the first click, specifically, 1st click events are removed. 

This method is much more efficient than the first method, and can be easily expanded to three-click, four-click ...... Event. For example:
<Pre name = "code" class = "java"> List <Long> times = new ArrayList <Long> (); public void click2 (View view) {times. add (SystemClock. uptimeMillis (); if (times. size () = 3) {if (times. get (times. size ()-1)-times. get (0) <500) {times. clear (); Toast. makeText (this, "three-click mode", 0 ). show ();} else {times. remove (0 );}}}


Only the judgment conditions are changed. Third, Google programmers. The following is the three-click method written by Google. I changed it to the double-click method. 
/*** Double-click an event or multi-Click Event * // an array of long [] mHits = new long [2]; public void doubleClick () {// double-click event response/*** arraycopy, copy the array * src Source array to be copied * srcPos source array start to copy the subscript position * dst target array * dstPos start to store the subscript position * length to copy the length (number of elements) ** // implement the array shift operation. Click one at a time and move one digit to the left to add the current Boot time (cpu time) System at the end. arraycopy (mHits, 1, mHits, 0, mHits. length-1); mHits [mHits. length-1] = SystemClock. uptimeMillis (); // double-click the event interval of 500 msif (mHits [0]> = (SystemClock. uptimeMillis ()-500) {// the specific operation after double-clicking // do }}
It is very concise and has similar ideas, but 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 length of the array.

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.