SoFunction
Updated on 2025-04-10

Detailed explanation of the code that implements multiple consecutive clicks on events

Sometimes we need to implement such a scenario, similar to entering the developer mode, that is, performing operations after multiple clicks.

First, let’s look at a method:

System provides a static method arraycopy(), which we can use to implement copying between arrays.

public static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length);

src: Source array;
srcPos: The starting position of the source array to be copied;
dest:Array of destination;
destPos: The starting position of the destination array;
length: the length of copy.

Note: src and dest must be of the same type or can be converted into arrays.

final static int COUNTS = 4;// Number of clicksfinal static long DURATION = 1000;// Specify the validity timelong[] mHits = new long[COUNTS];

First, we define the number of times, the specified validity time, and the corresponding array, that is, we need to click 4 times in one second to be valid.

  @Override
  public void onClick(View v) {
    continuousClick(COUNTS, DURATION);
  }
  private void continuousClick(int count, long time) {
    //Each click, the array moves one by one forward    (mHits, 1, mHits, 0,  - 1);
    // Assign a value to the last bit of the array    mHits[ - 1] = ();
    if (mHits[0] >= (() - DURATION)) {
      mHits = new long[COUNTS];//Reinitialize the array      (this, "4 consecutive clicks", Toast.LENGTH_LONG).show();
    }
  }

Idea: First, when we click, we move the array to the left one, assign time to the last digit. From the above code, we can see that when we click four times, the last digit has been moved to the first digit, and then we compare the time:

mHits[0] >= (() - DURATION)

If it is within the time specified by us, it will take effect and perform what we want.

(): The time it takes for the phone to be turned on.

Note: After performing the operation, you need to initialize the array from a new one:mHits = new long[COUNTS];Otherwise, the event will also be triggered when clicking the sixth and seventh time.

ps: Two and multiple click events of Android control

I simulated a Button double-click event myself, and how should I write a triple-click event? By checking the multiple clicks of Google Big Bull, I found that my poor student was really hard to match. . .

The principle of multiple click events: record the current time of each click event, judge the difference between the last click and the first click event. If it is less than 500ms (you can define this value yourself), it is considered to be a multiple click event. Let’s write a code using the 3 click event as an example.

1. The length of the mHits array is equivalent to the number of clicks. That is to say, the array length is now 3, and we can listen to events with 3 quick clicks.

2,(mHits, 1, mHits, 0, - 1);Copying the array, the first parameter: the array to be copied; the second parameter: where to start copying (here it starts from 1); the third parameter: the target array; the fourth parameter: where to start storage in the target array (here it starts from 0); the fifth parameter: the length of the copy. Through this method, we realize the recording of the time of each click event, and we can judge whether any 3 consecutive clicks are considered as 3-click events.

3,if (mHits[0] >= (mHits[ - 1] - 500)){}: This is used to determine whether it is a 3-click event, and to determine whether the time difference between the time of the click event with a subscript of 2 in the array and the time difference between the click event with a subscript of 0 in the array is less than 500; if it is less than 500, it is considered a 3-click event, and the processing is written in {}; otherwise, it is not a 3-click event.

package ;
import ;
import ;
import ;
import ;
import ;
public class MainActivity extends Activity {
 // The length of the array represents the number of clicks long[] mHits = new long[3];
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 (savedInstanceState);
 setContentView(.activity_main);
 }
 /**
  * Double-click event
  * @param v
  */
 public void click(View v) {
 // The array moves one by one in sequence (mHits, 1, mHits, 0,  - 1);
 mHits[ - 1] = ();// Run time after boot if (mHits[0] >= (mHits[ - 1] - 500)) {
  (this, "3 Combo", Toast.LENGTH_LONG).show();
 }
 }
}

Summarize

The above is a detailed explanation of the code of Android implementation of multiple consecutive clicks introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support for my website!