简体   繁体   中英

Implement double click for button in Android

How can I implement double click for a button in Android? Should I use OnDoubleTapListener?

int i = 0;
btn.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        i++;
        Handler handler = new Handler();
        Runnable r = new Runnable() {

            @Override
            public void run() {
                i = 0;
            }
        };

        if (i == 1) {
            //Single click
            handler.postDelayed(r, 250);
        } else if (i == 2) {
            //Double click
            i = 0;
            ShowDailog();
        }


    }
});
private long lastTouchTime = 0;
private long currentTouchTime = 0;

..

         @Override
                public void onClick(View view) {

                    lastTouchTime = currentTouchTime;
                    currentTouchTime = System.currentTimeMillis();

                    if (currentTouchTime - lastTouchTime < 250) {
                        Log.d("Duble","Click");
                        lastTouchTime = 0;
                        currentTouchTime = 0;
                    }

                }

This is probably a good place to start:

Android: How to detect double-tap?

I recommend switching to a more native way like long press (answer to linked question) or something more creative (using multi-touch), unless you are bent on the Windows default double-click way of doing things?

You may have a valid reason though - double clicking is after all faster than long press.

I wrote this for popping up a Toast message on a double click in a mapping application:

private long lastTouchTime = -1;

@Override
public boolean onTouchEvent(MotionEvent e, MapView mapView) {

   GeoPoint p = null;

   if (e.getAction() == MotionEvent.ACTION_DOWN) {

      long thisTime = System.currentTimeMillis();
      if (thisTime - lastTouchTime < 250) {

         // Double click
         p = mapView.getProjection().fromPixels((int) e.getX(), (int) e.getY());
         lastTouchTime = -1;

      } else {
         // too slow
         lastTouchTime = thisTime;
      }
   }
   if (p != null) {
      showClickedLocation(p);// Raise a Toast
   }
   return false;
}

This is a good site for performing double click... I used it and worked.

http://mobile.tutsplus.com/tutorials/android/android-gesture/

I used it and worked:

public class DoubleClickTest extends Activity {
    String TAG = "DoubleOrSingleClickTest";
    private boolean waitDouble = true;
    private static final int DOUBLE_CLICK_TIME = 350; // double click timer

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.double_click_test);
        Button button = (Button) findViewById(R.id.buttonDoubleOrSingleClicked);
        button.setOnClickListener(listenerDoubleOrSingle);
    }

    View.OnClickListener listenerDoubleOrSingle = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (waitDouble == true) {
                waitDouble = false;
                Thread thread = new Thread() {
                    @Override
                    public void run() {
                        try {
                            sleep(DOUBLE_CLICK_TIME);
                            if (waitDouble == false) {
                                waitDouble = true;
                                singleClick();
                            }
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                };
                thread.start();
            } else {
                waitDouble = true;
                doubleClick();
            }
        }
    };
    // single event
    private void singleClick() {
        Log.i(TAG, "singleClick");
    }
    // double event
    private void doubleClick() {
        Log.i(TAG, "doubleClick");
    }
}

It comes from " https://elingwange.iteye.com/blog/1613177 "

Create your own DoubleTapListener

You can create a DoubleTapListener by inheriting View.OnClickListener and adding a Callback of your listener.

MyDoubleClickListener.class

public class MyDoubleClickListener implements View.OnClickListener{

    private boolean isRunning= false;
    private int resetInTime =500;
    private int counter=0;

    private DoubleClickCallback listener;

    public DoubleTapListener(Context context)
    {
      listener = (DoubleClickCallback)context;             
    }

    @Override
    public void onClick(View v) {

       if(isRunning)
       {
          if(counter==1) //<-- makes sure that the callback is triggered on double click
          listener.onDoubleClick(v);
       }

       counter++;

       if(!isRunning)
       {
          isRunning=true;
          new Thread(new Runnable() {
              @Override
              public void run() {
                 try {
                    Thread.sleep(resetInTime);
                    isRunning = false;
                    counter=0;
                 } catch (InterruptedException e) {
                  e.printStackTrace();
                 }
              }
          }).start();
       }

    }

}

DoubleClickCallback.class

public interface DoubleClickCallback {

   public void onDoubleClick(View v);

}

And you are done. You can use this Listener in any Activity.

How do I use this DoubleClickListener in my Activity?

Implement Callback in your activity and override the method.

public class MainActivity extends AppCompatActivity implements MyDoubleClickListener{

    private Button button;       

    @Override
    protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);

         button   = (Button)findViewById(R.id.button);    
         button.setOnClickListener(new DoubleTapListener(this));  //<-- Set listener

    }

    @Override
    public void onDoubleClick(View v) {
          // Toast to show double click        
    }

}

Important point is using this concept you can create any kind of listener (Triple-click listener)

Relevant Links:

See the full working code HERE

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM