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 ();}}