Android multiple click event

Android does not provide double-click or multiple-click events this time, and we need to implement it ourselves.

    Idea: Use an array to record the system time when the click occurs. If the current click event occurs 3 times and the difference between the current system time and the time when the first click event occurs is less than 500, it is considered to be a continuous click and 3 clicks are made. Second-rate.

    The number of clicks is cleverly controlled by using an array, because the default initial value of the array is all 0. Each click will assign a value to the last element of the array, and then move all elements of the array to the left by one, and move the one of the first click event each time The value is shifted to the left by one bit. If it is shifted to the left 3 times, mHints[0] must not be 0. If mHints[0] is 0, it means that it has not been clicked 3 times. If mHints[0] is not equal to 0, it means that the array has moved 3. 3 click events occurred

     The next step is to consider whether it is a continuous click, assuming that the system time for each click is the following value.

     Array initial: 0 0 0

     First click: 0 0 1020

     Second click 0 1020 1050

     Third click 1020 1050 1070

     After the third click, the if judgment statement assumes that the current current system time is 1075

     if (SystemClock.uptimeMillis()-mHints[0]<=500)

     This line of statement is 1075-1020<=500, 1020 is the time of the first click, the difference between the time 1075 after the third click and the time 1020 of the first click is the time it takes for 3 clicks , if the difference is less than 500, we consider it a continuous click

package com.passion.mobilesafe;
import android.app.Activity;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
/**
 * Created by passion on 2015/8/10.
 */
public class MultiClickActivity extends Activity
{
    private TextView tv_name;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        tv_name= (TextView) findViewById(R.id.tv_toast_address);
        tv_name.setOnClickListener(new View.OnClickListener()
        {
           //The length of the array needs to be monitored for several click events.
            //If you want to listen for double-click events, the length of the array is 2. If you want to listen for 3 consecutive click events, the length of the array is 3...
            long[] mHints = new long [3];//Initial all 0
            @Override
            public void onClick(View v)
            {
                //Move all elements in the mHints array to the left by one position
                System.arraycopy(mHints, 1, mHints, 0, mHints.length - 1 );
               //Get the time when the current system has been started
                mHints[mHints.length - 1] = SystemClock.uptimeMillis();
                if (SystemClock.uptimeMillis()-mHints[0]<=500)
                    Toast.makeText(MultiClickActivity.this , "3 clicks", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325581245&siteId=291194637