Double-click, Triple-click, and multiple-click events

Source: Internet
Author: User

First, the goal

1. Implement Double click event

2. Achieve three hits and more click events

Second, the Code implementation

The first method:

1. Define a long variable (named Firstclicktime) to store the time of the first click;

2, in the Click event, through the IF statement to determine whether the first click Time (firstclicktime) is greater than 0, if it is established in the IF statement to create a long type of variable (named Secondclicktime), to store the second click Time, Then use the IF statement to determine whether the two-click time difference is less than 500 milliseconds. If it is set up, the corresponding statement is executed; if it is greater than 500 milliseconds, the time value (Firstclicktime) of the first click is re-assigned to 0, and then returned.

3, the system clock (systemclock) to obtain the power-on millisecond value Uptimemillis () method (that is, the number of milliseconds to boot from the system to the current time), and a long type of variable record (named Firstclicktime);

Code:

4, the above steps exist "click once and then double-click No effect" problem. Therefore, by creating a new sub-thread, the child line thread sleep 500 milliseconds, and then the first click Time Value (Firstclicktime) is re-assigned to 0;

The first method of implementation click event code:

1  Public voidClick (View view) {2         if(Firstclicktime > 0){3             LongSecondclicktime =Systemclock.uptimemillis ();4             if(secondclicktime-firstclicktime<500){5Toast.maketext ( This, "Double click succeeded", 1). Show ();6}Else {7Firstclicktime = 0;8             }9             return;Ten         } One         //after the child thread sleeps 500 and then assigns the first click Time to 0, prevents the occurrence after clicking once and then double-clicking the no effect phenomenon AFirstclicktime =Systemclock.uptimemillis ();  -         NewThread () { -              Public voidrun () { the                 Try { -Thread.Sleep (500); -}Catch(interruptedexception e) { - e.printstacktrace (); +                 } -Firstclicktime = 0; +             }; A }.start (); at}
View Code

5, three-click and multiple clicks can be achieved by adding variables, intermediate values, etc., but the variables, intermediate values, etc., especially against multiple (such as 10 times) click.

The second method (refer to the system source code in Deviceinfosettings.java Onpreferencetreeclick (...) Code in the method:

1, the definition of a long array of objects (named mHITs, the length of a temporary 3, indicating a click of 3 times, the length is to click the number of times), the array is used to store the time of each click;

2. In the Click event Method (click View):

(1) Copy arraycopy (object src, int srcpos, object DST, int dstpos, int length) through the system (systems) method to deposit the time of each click into the last digit of the array in 1, The method parameter src represents the original array of copies, the Srcpos from where the array is copied, the DST target array, where the Dstpos is copied to the destination array, and the number of elements of the data for the length copy.

Code for Step (1):

1 system.arraycopy (mhits, 1, mhits, 0, mhits.length-1);
View Code

The actual effect of the above code is to move the array in 1 to the left one

(2) The Uptimemillis () method of the system clock (systemclock) assigns the millisecond value of the current time to the system boot time to the last element of the array in 1 (that is, the array length-1);

(3) Determine whether the difference between the last and first digit of the array in 1 is less than or equal to the required time limit (note: The unit is in milliseconds).

3. If the conditions in the IF statement are met, the relevant statement code is executed in the IF statement.

The second method implements the Click event code:

 1  public  void   Click (View view) { //  3  mhits[mhits.length-1] = Systemclock.uptimemi Llis ();  4  if  ((mhits[mhits.length-1]-mhits[0)) <= 500 this , mhits.length+ "Times clicked", 1). Show ();  6   7
       } 
View Code

Double-click, Triple-click, and multiple-click events

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.