Method steps for Android single double click implementation

  • 2021-11-14 07:16:01
  • OfStack

Record click, double-click implementation process, simple encapsulation, easy to reuse, including common software double-click exit.

Double-click realization: record the first click time, click again within the set time, then return to monitor events, otherwise do not do processing; Application double-click exit is the same implementation logic.


/**
 *  Double-click to implement 
 *
 * @author  Several rings 
 */
public abstract class BaseDoubleClickListener implements View.OnClickListener {

  private static final long DOUBLE_TIME = 500;
  private static long lastClickTime = 0;

  @Override
  public void onClick(View v) {
    long currentTimeMillis = System.currentTimeMillis();
    if (currentTimeMillis - lastClickTime < DOUBLE_TIME) {
      onDoubleClick(v);
    }
    lastClickTime = currentTimeMillis;
  }

  /**
   *  Double-click event 
   *
   * @param v  View 
   */
  public abstract void onDoubleClick(View v);

}

Call mode:


view.setOnClickListener(new BaseDoubleClickListener() {
  @Override
  public void onDoubleClick(View v) {
    
  }
});

Application Double-click to exit:


private long onBackPressedTime;
@Override
public void onBackPressed() {
  long timeSpan = System.currentTimeMillis() - onBackPressedTime;
  onBackPressedTime = System.currentTimeMillis();
  if (timeSpan > 2000) {
    Toast.makeText(this, getResources().getString(R.string.exit), Toast.LENGTH_SHORT).show();
  } else {
    super.onBackPressed();
  }
}

Click and double-click realization: use Handler to send delayed messages, get the number of clicks by counting, and return the double-click event if you click twice within the set time; Single time, the click event is returned.


/**
 *  Click, double-click to implement 
 *
 * @author  Several rings 
 */

public abstract class BaseClickListener implements View.OnClickListener {

  private static final int TIMEOUT = 400;

  private int clickCount = 0;
  private Handler handler;

  protected BaseClickListener() {
    handler = new Handler();
  }

  @Override
  public void onClick(View v) {
    clickCount++;
    handler.postDelayed(new Runnable() {
      @Override
      public void run() {
        if (clickCount == 1) {
          onSingleClick(v);
        } else if (clickCount == 2) {
          onDoubleClick(v);
        }
        handler.removeCallbacksAndMessages(null);
        clickCount = 0;
      }
    }, TIMEOUT);
  }

  /**
   *  Click to implement 
   *
   * @param v  View 
   */
  public abstract void onSingleClick(View v);

  /**
   *  Double-click to implement 
   *
   * @param v  View 
   */
  public abstract void onDoubleClick(View v);

}

Call mode:


view.setOnClickListener(new BaseClickListener() {
  
  @Override
  public void onSingleClick(View v) {

  }

  @Override
  public void onDoubleClick(View v) {
   
  }
});

The OnClickListener event of View is mainly rewritten, and OnTouchListener can also be rewritten for processing; The abstract class abstraction method is adopted, and the interface encapsulation can also be used for processing.


Related articles: