how to implement double click in android

前端 未结 9 810
深忆病人
深忆病人 2020-12-05 20:36

I am doing a project in which i want to display a particular message on single touch and another message on double touch using android.How can i implement it.

My sa

相关标签:
9条回答
  • 2020-12-05 21:15

    I have Implemented the code on which the Background color and text color changes on clicking the screen twice(Double Tap) here is the code...

            int i = 0;
            reader.setOnClickListener(new View.OnClickListener() {
                int i = 0;
                @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) {
    
                        if(color==1) {
    
                            reader.setTextColor(0xFF000000);
                            reader.setBackgroundColor(0xFFFFFFFF);
                            color = 2;
                        }else if(color==2)
                        {
                            reader.setTextColor(0xFFFFFFFF);
                            reader.setBackgroundColor(0xFF000000);
                            color=1;
                        }
                        i = 0;
    
                    }
    
    
                }
            });
    
    0 讨论(0)
  • 2020-12-05 21:18

    Subclass View.OnClickListener

    I have created a DoubleClickListener using an interface and a Thread. It can be used with any View just like classic ClickListener. This is how you can add my listener for double click events.

    Example,

    button.setOnClickListener(new DoubleTapListener(this));
    

    And in any Activity, Override this method to perform task

    @Override
    public void onDoubleClick(View v) {
        // Do what you like
    }
    

    Implementation:

    Let's see the implementation. I have created a simple DoubleTapListener but using this approach you can create complex touch listeners(Tripletouch Listener, single, double, triple listener all in one).

    1) DoubleTapListener

    public class DoubleTapListener  implements View.OnClickListener{
    
          private boolean isRunning= false;
          private int resetInTime =500;
          private int counter=0;
    
          private DoubleTapCallback listener;
    
          public DoubleTapListener(Context context)
          {
              listener = (DoubleTapCallback)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();
             }
    
          }
    
    }
    

    2) DoubleTapCallback

    public interface DoubleTapCallback {
    
         public void onDoubleClick(View v);
    }
    

    3) Implement callback in your activity and override the method

    public class MainActivity extends AppCompatActivity implements DoubleTapCallback{
    
          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        
          }
     }
    

    Relevant link:

    You can see the full code implementation HERE

    0 讨论(0)
  • 2020-12-05 21:21

    Why dont you use Long Press event insted while its Recommanded UI. Read Answer Here , I strongly recommand to use this.

    Or if its anyhow you want to implement you have two options , one is this using boolean and second is using Gesture Listener.

    0 讨论(0)
  • 2020-12-05 21:22

    You may employ RxAndroid in that case in Kotlin it shall be like this:

    yourView.clicks().buffer(500, TimeUnit.MILLISECONDS, 2).filter {
        it.size >= 2
    }.subscribe {
        // Handle double click
    }
    
    1. We apply clicks() extension function on a given view which creates an RX observable sequence.
    2. We tell RX to emit an event after either 500 ms or 2 consecutive clicks take place within 500 ms.
    3. We tell RX to take only the events only with 2 consecutive click
    4. Last but not least we subscribe to the event sequence which is our DoubleClick handler
    0 讨论(0)
  • 2020-12-05 21:22

    Try the below modified code::

    //Check that thisTime is greater than prevTime
    //just incase system clock reset to zero
    static prevTime = 0;
    thisTime = Calendar.getInstance().getTimeInMillis();
    if(prevTime < thisTime) {
        //Check if times are within our max delay
        if((thisTime - prevTime) <= 1000) {  //1 SEC
            //We have detected a double tap!
            Toast.makeText(AddLocation.this, "DOUBLE TAP DETECTED!!!", Toast.LENGTH_LONG).show();
            prevTime = thisTime;
            //PUT YOUR LOGIC HERE!!!!
        } else {
            //Otherwise Reset firstTap
            firstTap = true;
        }
    } else {
        firstTap = true;
    }
    
    0 讨论(0)
  • 2020-12-05 21:25

    It is hold, but,for new readers, I made a small library to simplify this kind of stuff, check this article: Double click listener on android.

    The library is very small, and here is the GitHub Repository Link.

    And you will just use it like this:

    Button btn = new Button(this);
    
    btn.setOnClickListener( new DoubleClick(new DoubleClickListener() {
            @Override
            public void onSingleClick(View view) {
    
                // Single tap here.
            }
    
            @Override
            public void onDoubleClick(View view) {
    
                // Double tap here.
            }
        });
    
    0 讨论(0)
提交回复
热议问题