Detailed explanation of Android code for realizing continuous click multiple events

  • 2021-10-16 04:54:38
  • OfStack

Sometimes we need to implement such scenarios, which are similar to entering developer mode, that is, performing operations after multiple clicks.

First, let's look at one method:

System provides a static method, arraycopy (), which we can use to replicate 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: Destination array;
destPos: The starting position of the destination array placement;
length: The length of the copy.

Note: src and dest must all be arrays of the same type or convertible type.


final static int COUNTS = 4;//  Number of clicks 
final static long DURATION = 1000;//  Specified effective time 
long[] mHits = new long[COUNTS];

First, we define the number of times, the specified effective time, and the corresponding array, that is, we have to click four times in one second to be effective


  @Override
  public void onClick(View v) {
    continuousClick(COUNTS, DURATION);
  }
  private void continuousClick(int count, long time) {
    // With each click, the array moves forward 1 Bit 
    System.arraycopy(mHits, 1, mHits, 0, mHits.length - 1);
    // Is the last of the array 1 Bit assignment 
    mHits[mHits.length - 1] = SystemClock.uptimeMillis();
    if (mHits[0] >= (SystemClock.uptimeMillis() - DURATION)) {
      mHits = new long[COUNTS];// Reinitialize the array 
      Toast.makeText(this, " Clicked continuously 4 Times ", Toast.LENGTH_LONG).show();
    }
  }

Thinking: First, when we click, we all move the array to the left by one bit, and assign the time to the last one bit. From the above code, we can see that when we click 4 times, the last one bit has been moved to the first place, and then we compare the time:


mHits[0] >= (SystemClock.uptimeMillis() - DURATION)

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

SystemClock. uptimeMillis (): The time since the phone was turned on.

Note: You need to reinitialize the array after performing the operation: mHits = new long[COUNTS]; Otherwise, clicking the 6th and 7th times will also trigger the event.

ps: Double and Multiple Click Events for Android Controls

I simulated a double-click event of Button. How to write the 3-click event? By looking at Google Daniel's many clicks, I found that my scum is really hard to match. . .

Principle of multiple click events: Record the current time of each click event, and judge the time 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 as a multiple click event. Let's write a code with 3 click events as an example.

1. The length of mHits array is equivalent to the number of clicks. That is to say, the array length is now 3, and we can listen for events that are clicked 3 times quickly.

2. System.arraycopy(mHits, 1, mHits, 0, mHits.length - 1); Copy of array, the first parameter: the array to be copied; The second parameter: where to start copying (in this case, starting from 1); Parameter 3: Target array; The fourth parameter: where to start the storage in the target array (here, starting from 0); Parameter 5: The length of the copy. Through this method, we can record the time of every one click event, and judge whether any three consecutive clicks are regarded as three-click events.

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


package cn.com.cyj.doubleclick;
import android.app.Activity;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.widget.Toast;
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) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 }
 /**
 *  Double-click event 
 * @param v
 */
 public void click(View v) {
 //  Arrays move in sequence 1 Bit 
 System.arraycopy(mHits, 1, mHits, 0, mHits.length - 1);
 mHits[mHits.length - 1] = SystemClock.uptimeMillis();//  Running time after boot 
 if (mHits[0] >= (mHits[mHits.length - 1] - 500)) {
  Toast.makeText(this, "3 Combo ", Toast.LENGTH_LONG).show();
 }
 }
}

Summarize


Related articles: