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.