Differentiate Single click and double click of a imageView in android

前端 未结 5 992
南笙
南笙 2021-01-29 10:24

I have tried the following code to differentiate single click and double click. Single click is ok. When I double click the imageview, code inside both the single click and doub

5条回答
  •  南笙
    南笙 (楼主)
    2021-01-29 10:58

    btn.setOnClickListener(new View.OnClickListener() {
    
                                   volatile int i = 0;
    
                                   @Override
                                   public void onClick(View v) {
    
                                       i++;
                                       Handler handler = new Handler();
    
                                       Runnable r = new Runnable() {
                                           @Override
                                           public void run() {
                                               if (i == 1) {
                                                   //single click logic
    
                                                   i = 0; //set to zero 
                                               }
                                           }
                                       };
    
                                       if (i == 1) {
                                           handler.postDelayed(r, 300);
                                       } else if (i == 2) {
                                           handler.removeCallbacks(r);
                                           i = 0;
                                           //Double click logic
    
                                       }
                                   }
                               }
        );
    

提交回复
热议问题