Java uses arraycopy to implement multiple-hit event _java

Source: Internet
Author: User
Tags time interval

The example of this article for everyone to share the Java arraycopy to achieve a multiple-hit event of 3 ways for everyone to refer to, the specific contents are as follows

1, double-click the implementation of the event

We specify that the event interval of two clicks is a double click event within 500 milliseconds, and this value can be arbitrarily qualified.

    Bt_click.setonclicklistener (New Onclicklistener () {
      @Override public
      void OnClick (View v) {
        if starttime !=0) {
          Long endtime = System.currenttimemillis ();
          if (endtime-starttime<500) {
            toast.maketext (Getapplicationcontext (), "two clicks", 0). Show ();
          }
        StartTime = System.currenttimemillis ();
      }
    );

2. Arraycopy usage

Arraycopy is a function for array replication

Let's take a look at a small example of arraycopy.

    Static initialization of two different lengths of array
    int src[] = {1,2,3,4,5,6};
    int dest[] = {10,9,8,7,6,5,4,3,2,1};
    Copy the 4 elements of the array src into the array dest
    system.arraycopy (src,1,dest,2,4);

    Output array dest for
    (int i=0;i<10;i++)
    {
      System.out.println (dest[i]);
    }

Output results

From the result we can see the use of arraycopy

Parameters:

1. The original array (the array to be copied)
2. Index value of the copy starting position of the original array
3. Target array (data from the original array-copy –> target array)
4. The starting index position of the target array accepted value
5. Length of Copy
-

3, the implementation of multiple-hit event

Private long[] mHITs = new Long[3];
    Bt_many_click.setonclicklistener (New Onclicklistener () {
      @Override public
      void OnClick (View v) {
        System.arraycopy (mhits, 1, mhits, 0, mhits.length-1);
        Mhits[mhits.length-1] = Systemclock.uptimemillis ();
        if (mhits[mhits.length-1]-mhits[0]<500) {
          //response to a three-click event
          toast.maketext (Getapplicationcontext (), "clicked three!!!!" , 0). Show ();}}
    ;

mHITs array with a length of 3 (that is, multiple hits), and the last Mhits[mhits.length-1] stores the time of each click

Arraycopy once per click

When the click is enough 3 times that mhits[0] has a value to determine the last click and the first click of the time interval, less than our limited time is determined to three hits.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.