Android Monitor continuous clicks Code Implementation

Source: Internet
Author: User

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

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.