Code implementation for consecutive clicks of Android listeners

Source: Internet
Author: User

Code implementation for consecutive clicks of Android listeners
General implementation

We know that double-click events are generally implemented. You can use new OnClickListener () to listen to click events, and then record the time between the two clicks before and after the start. The time difference is within a certain range. The Code is as follows:

Long firstClickTime = 0; @ Overridepublic void onClick (View view) {if (firstClickTime> 0) {long secondClickTime = SystemClock. uptimeMillis (); // long dtime = secondClickTime-firstClickTime; if (dtime> 500) {Toast. makeText (getApplicationContext (), "implement double-click event listening", 0 ). show () ;}else {firstClickTime = 0;} return ;}firstclicktime = SystemClock. uptimeMillis ();}
Algorithm implemented by Google

After reading the above implementation, the idea is very simple, but when you need to implement multiple clicks, the above Code may be large. Here we will write down the method provided by GoogleAPI.

// Array Storage clicks long [] mHits = new long [2]; @ Overridepublic void onClick (View v) {// double-click method // source array copied by src // srcPos starts copying from the position of the source array. // dst target array // dstPos writes data from the position of the target array // The number of elements copied by length System. arraycopy (mHits, 1, mHits, 0, mHits. length-1); // shift left, and then update the start time from the last position. If the last time and start time are less than 500, double-click mHits [mHits. length-1] = SystemClock. uptimeMillis (); if (mHits [0]> = (SystemClock. uptimeMillis ()-500) {// double-click center... Update the window and save lastXparams. x = wm. getdefadisplay display (). getWidth ()/2-view.getWidth ()/2; wm. updateViewLayout (view, params); Editor editor = sp. edit (); editor. putInt ("lastx", params. x); editor. commit ();}}



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.