This article has shared with you three ways to implement multi-click events using arraycopy for your reference. The specific content is as follows
1. Implementation of double-click events
We stipulate that the interval between the two clicks is a double-click event within 500 milliseconds, and this value can be limited at will.
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(), "clicked twice", 0).show(); } } startTime = System.currentTimeMillis(); } });2. Usage of arraycopy
arraycopy is a function used for array copying
Let's first look at a small example of arraycopy
// Statically initialize two arrays of different lengths 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 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 result
From the results we can see the usage of arraycopy
parameter:
1. Original array (array to be copied)
2. The index value of the copy start position of the original array
3. Target array (the data of the original array - copy > Target array)
4. The starting index position of the target array accepts the value
5. Copy length
-
3. Implementation of multi-strike events
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){ //Responses to a three-click event Toast.makeText(getApplicationContext(), "Clicked three times!!!", 0).show(); } } });mHits array with length 3 (i.e., multiple hits), the last bit mHits[mHits.length-1] stores the time of each click
Arraycopy once per click
When the time interval between the last click and the first click is determined after the mHits[0] has a value, it is determined as three hits if the time is less than our limited time.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.