2011-01-31 12 views

Respuesta

4

Esto es probablemente un buen lugar para empezar:

Android: How to detect double-tap?

Yo recomiendo cambiar a una forma más similar a la nativa pulsación larga (respuesta a la pregunta ligada) o algo más creativo (con multi-touch), a menos que esté inclinado en la forma predeterminada de Windows, haga doble clic en la forma de hacer las cosas?

Aunque puede que tenga un motivo válido: hacer doble clic en es después de todo, más rápido que presionar durante mucho tiempo.

28
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(); 
     } 


    } 
}); 
+2

Esto no toma en cuenta las limitaciones de tiempo [] (http://stackoverflow.com/a/15353499/636571). – an00b

+0

@ an00b comprueba la respuesta actualizada con restricciones de tiempo –

+0

Ah, lo tengo. +1. – an00b

3

escribí esto para aparecer un mensaje en tostada por un doble clic en una aplicación de mapas:

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; 
} 
1
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; 
        } 

       } 
Cuestiones relacionadas