General implementation
We know that the general implementation of the double-click event, you can listen to the Click event through New Onclicklistener (), and then record the time before and after two clicks, by the difference in a certain range implementation, the code is as follows:
Long firstclicktime = 0; @Overridepublic void OnClick (view view) {if (Firstclicktime > 0) {Long secondclicktime = SYSTEMCL Ock.uptimemillis ()///distance from last boot time long dtime = Secondclicktime-firstclicktime;if (Dtime >) {toast.maketext ( Getapplicationcontext (), "Implement double-click event Listener", 0). Show (); Else{firstclicktime = 0;} return; }firstclicktime = Systemclock.uptimemillis ();}
Google implementation algorithm
After reading the above implementation, the idea is very simple, but when to achieve multiple clicks, the above may be a large amount of code, here write down the method provided by Googleapi
<span style= "White-space:pre" ></span>//array storage clicks long[] mhits = new long[2]; @Overridepublic void OnClick (View v) {///implement a double-click Method//src copy of the source array//srcpos start copying from that location in the source array.//DST target array//dstpos start writing data from the seat of the target array//length The number of elements copied system.arraycopy (mHITs, 1, mhits, 0, mhits.length-1);//implement left SHIFT, and then the last position to update the time from the boot, if the last time and the start time is less than 500, double-click mHITs [Mhits.length-1] = Systemclock.uptimemillis (); if (Mhits[0] >= (Systemclock.uptimemillis ()-500) {//double-click Center ... Half of the screen and half of the place of attribution, Update window, save lastxparams.x = Wm.getdefaultdisplay (). GetWidth ()/2-view.getwidth ()/2;wm.updateviewlayout ( view, params); Editor editor = Sp.edit (); Editor.putint ("Lastx", params.x); Editor.commit ();}}
Android Monitor continuous clicks Code Implementation