2010-07-14 81 views

回答

2

我確定那裏的所有代碼都是確定第二次點擊是否在第一次點擊的特定時間內,否則將其視爲第二次點擊。無論如何,我都會這麼做。

1

只是使用setOnTouchListener記錄第一次和第二次點擊時間。如果他們非常接近,請將其確定爲雙擊。與此類似,

public class MyActivity extends Activity { 

    private final String DEBUG_TAG= "MyActivity"; 
    private long firstClick; 
    private long lastClick; 
    private int count; // to count click times 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     Button mButton= (Button)findViewById(R.id.my_button); 
     mButton.setOnTouchListener(new View.OnTouchListener() { 
      @Override 
      public boolean onTouch(View view, MotionEvent motionEvent) { 
       switch (motionEvent.getAction()) { 
        case MotionEvent.ACTION_DOWN: 
         // if the second happens too late, regard it as first click 
         if (firstClick != 0 && System.currentTimeMillis() - firstClick > 300) { 
          count = 0; 
         } 
         count++; 
         if (count == 1) { 
          firstClick = System.currentTimeMillis(); 
         } else if (count == 2) { 
          lastClick = System.currentTimeMillis(); 
          // if these two clicks is closer than 300 millis second 
          if (lastClick - firstClick < 300) { 
           Log.d(DEBUG_TAG,"a double click happened"); 
          } 
         } 
         break; 
        case MotionEvent.ACTION_MOVE: 
         break; 
        case MotionEvent.ACTION_UP: 
         break; 
       } 
       return true; 
      } 
     }); 
    } 
}