android:实现双击事件(DoubleClick)

public class DoubleClickTest extends Activity {

   String TAG = "DoubleClickTest";
   
   private boolean waitDouble = true;
   private static final int DOUBLE_CLICK_TIME = 350; //两次单击的时间间隔
   
   @Override
   public void onCreate(Bundle savedInstanceState){
      super.onCreate(savedInstanceState);
      
      // 设置布局文件,其中只有一个Button
      setContentView(R.layout.double_click_test);
      
      Button button = (Button)findViewById(R.id.button);
      button.setOnClickListener(listener);
   }
   
   
   OnClickListener listener = new 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();
         }
      }
   };
   
   // 单击响应事件
   private void singleClick(){
	   Log.i(TAG, "singleClick");
   }
   
   // 双击响应事件
   private void doubleClick(){
	   Log.i(TAG, "doubleClick");
   }
   
}


缺点:用延时来确定双击,因为开了新的线程,对主线程UI操作不便

 
  

你可能感兴趣的:(android)