Multiple Click events for Android Development

Source: Internet
Author: User

We have a single click event in Android. But it doesn't give us a double-click, or implement multiple events within a certain amount of time. So we need to implement the modification on the single monitor.

There are two ways to achieve this:

1. Define a variable that stores the first click, and if the two time interval is less than 500 milliseconds, it is considered a double-click time.

The implementation is as follows:

Package Com.andy.doubleclick;import Android.app.activity;import Android.os.bundle;import android.os.SystemClock; Import Android.view.view;import android.widget.toast;/** * @author zhang,tianyou * @version December 02, 2014 10:51:56 * * public class Mainactivity extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main);} Private Long startclicktime;public void click (View view) {Long nextclicktime = Systemclock.uptimemillis (); Startclicktime <= 0) {startclicktime = Systemclock.uptimemillis (); return;} else {if (Nextclicktime-startclicktime <) {Toast.maketext (this, "was double-clicked", Toast.length_short). Show (); Startclicktime = 0L;} else {startclicktime = Systemclock.uptimemillis ();}}}}

This approach has a flaw, if you want to achieve multiple clicks, then you need to define the storage of multiple event points of the variable, it is obviously not suitable for multiple click Processing.


2. Use the algorithm used in the API provided by Google.

To be able to implement N-click events, we need to define an array of n-lengths, each time the contents of the array are moved to the left by a single box, and then the n-1 is the last time the array is added, if the 0 positions are longer than the current time minus 500 milliseconds, That proves to have clicked N times within 500 milliseconds.


The implementation is as follows:

Package Com.andy.doubleclick;import Android.app.activity;import Android.os.bundle;import android.os.SystemClock; Import Android.view.view;import android.widget.toast;/** * @author zhang,tianyou * @version December 02, 2014 10:51:56 * * public class Mainactivity extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main);} long[] mhits = new long[2];p ublic void Click (View view) {//Per click implementation to move left one block of data system.arraycopy (mHITs, 1, mhits, 0, Mhits.length -1);//give the last assigned current clock value of the array mhits[mhits.length-1] = Systemclock.uptimemillis (),////////When the value of 0 out is greater than the current time-500,  it is shown that 2 times in 500 seconds have been clicked if ( Mhits[0] > Systemclock.uptimemillis () () {Toast.maketext (this, "was double-clicked", Toast.length_short). Show ();}}

This can be achieved by clicking on the event N, simply defining the array length as n length.


The difference between System.currenttimemillis () and Systemclock.uptimemillis ():

That's what the API says:

  • System.currentTimeMillis()Is the "Wall" clock (time and date) expressing milliseconds since the epoch. The wall clock can be set by the user or the phone network (see setCurrentTimeMillis ), so the time may jump backwards or forwards unpredic tably. This clock should only being used when correspondence with real-world dates and times are important, such as in a calendar or Alarm Clock application. Interval or elapsed time measurements should use a different clock. If you is using System.currenttimemillis (), consider listening ACTION_TIME_TICK to the, ACTION_TIME_CHANGED and ACTION_TIMEZONE_CHANGED Intent broadcasts to find out W Hen the time changes.

  • systemclock.uptimemillis is counted-milliseconds since the system was booted. This clock stops while the system enters deep sleep (CPU off, display dark, device waiting for external input) Affected by clock scaling, idle, or other power saving mechanisms. The basis for most interval timing such as thread.sleep (millls) , object.wait (Millis) , an D system.nanotime () . This clock was guaranteed to be monotonic, and was suitable for interval timing when the interval does not span device sleep . Most methods that accept a timestamp value currently expect the Uptimemillis clock.

  • SystemClock.elapsedRealtimeelapsedRealtimeNanosand return the time since the system was booted, and include deep sleep. This clock was guaranteed to being monotonic, and continues to the tick even when the CPU was in power saving modes, so is the Reco Mmend basis for general purpose interval timing.

SystemClock.elapsedRealtime: From power on to the current millisecond (sleep) time is also included in the phone

System.currentTimeMillis(): From January 1, 1970 UTC to the current number of milliseconds, can be modified by System.setcurrenttimemillis, then, in some cases, one but modified, the time interval is not allowed.

SystemClock.uptimeMillis: It represents the time the phone is running from startup to the present, and does not include the system sleep (CPU off), many system internal time is based on this, for example, Thread.sleep(millls) Object.wait(millis) and System.nanoTime() it represents the phone from the start to the current run time, And does not include the time of the system sleep (CPU off), the internal time of many systems is based on this, for example, Thread.sleep(millls) Object.wait(millis) andSystem.nanoTime()




Multiple Click events for Android Development

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.