Single click and double click of a button in android

前端 未结 9 1294
遇见更好的自我
遇见更好的自我 2020-12-05 22:09

In my application i have a button. After single and double clicking of the button will perform separate operation. How can i do that? Thanks

相关标签:
9条回答
  • 2020-12-05 22:11

    Thanks to @NikolaDespotoski.

    You can check DOUBLE-TAP example from following URL. that is used in listView. i hope it is useful for you.

    https://nodeload.github.com/NikolaDespotoski/DoubleTapListView/zip/master

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

    You have to implement GestureDetector and put your code in single/double click.

    TestActivity.java

    iv.setOnClickListener(new OnClickListener() {           
                    @Override
                    public void onClick(View v) {
                        //putyour first activity call.
                    }
        }
    
    iv.setOnTouchListener(new OnTouchListener() {
                 GestureDetector gestureDetector = new GestureDetector(context, new MyGestureDetector(context));
             @Override
             public boolean onTouch(View v, MotionEvent event) {
                    return gestureDetector.onTouchEvent(event);
             }
    });
    

    Now you have to create GestureDetector.java class.

    public class MyGestureDetector extends SimpleOnGestureListener {
        public Context context;
        public String phno;
    
        public MyGestureDetector(Context con) {
            this.context=con;       
        }
    
        @Override
        public boolean onDown(MotionEvent e) {
            return super.onDown(e);
        }
    
        @Override
        public boolean onDoubleTap(MotionEvent e) {
            System.out.println("in Double tap");
    
            return true;
        }
    
        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            System.out.println("in single tap up");
                //put your second activity.
            return super.onSingleTapUp(e);
        }   
    }
    
    0 讨论(0)
  • 2020-12-05 22:18

    I also got the same problem once

    setOnClickListener(new OnClickListener() {           
                @Override
                public void onClick(View v) {
                    if(isFmOn()){
                       //stopFM
                    }else{
                      //do other things
                    }
                }
    }
    

    when I clicked the Button,FM stopped;but when I double clicked,FM did not stop.The problem was that single and double clicking of the button ,the value of isFmOn() was difference. I sloved the problem using this:

    setOnClickListener(new OnClickListener() {           
                @Override
                public void onClick(View v) {
                    Thread.sleep(500);//500ms was enough to finish stopFM before the second click
                    if(isFmOn()){
                       //stopFM
                    }else{
                      //do other things
                    }
                }
    }
    
    0 讨论(0)
  • 2020-12-05 22:20

    Though it's too late, but anyone can figure out if they see this.

    int number_of_clicks = 0;
    boolean thread_started = false;
    final int DELAY_BETWEEN_CLICKS_IN_MILLISECONDS = 250;
    
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ++number_of_clicks;
            if(!thread_started){
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        thread_started = true;
                        try {
                            Thread.sleep(DELAY_BETWEEN_CLICKS_IN_MILLISECONDS);
                            if(number_of_clicks == 1){
                                client.send(AppHelper.FORMAT_LEFT_CLICK);
                            } else if(number_of_clicks == 2){
                                client.send(AppHelper.FORMAT_DOUBLE_CLICK);
                            }
                            number_of_clicks = 0;
                            thread_started = false;
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
            }
        }
    });
    

    Explanation:


    Before the button click, number_of_click is initialized to 0. thread_started is a flag detecting if the thread is started before or not. Now, on button click, increase the number of button click by incremental operator. check if the thread is previously started or not, if not, then start the thread. on thread, apply your logic by using the number_of_clicks. and the thread will wait for next milliseconds and then will go through your logic. So, now you can apply as many clicks as you want.

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

    You may need to create a delay variable which will differenciate between single click and double click.

    See this code,

    private static final long DOUBLE_PRESS_INTERVAL = 250; // in millis
    private long lastPressTime;
    
    private boolean mHasDoubleClicked = false;
    
        @Override
        public boolean onPrepareOptionsMenu(Menu menu) {    
    
            // Get current time in nano seconds.
            long pressTime = System.currentTimeMillis();
    
    
            // If double click...
            if (pressTime - lastPressTime <= DOUBLE_PRESS_INTERVAL) {
                Toast.makeText(getApplicationContext(), "Double Click Event", Toast.LENGTH_SHORT).show();
                mHasDoubleClicked = true;
            }
            else {     // If not double click....
                mHasDoubleClicked = false;
                Handler myHandler = new Handler() {
                     public void handleMessage(Message m) {
                          if (!mHasDoubleClicked) {
                                Toast.makeText(getApplicationContext(), "Single Click Event", Toast.LENGTH_SHORT).show();
                          }
                     }
                };
                Message m = new Message();
                myHandler.sendMessageDelayed(m,DOUBLE_PRESS_INTERVAL);
            }
            // record the last time the menu button was pressed.
            lastPressTime = pressTime;      
            return true;
        }
    
    0 讨论(0)
  • 2020-12-05 22:28

    Solving this by inherit from the View.OnClickListener and checking the click time to distinguish the single click or double click, this also solve the problem of fast clicking. This solution will bring minor code change, just replace View.OnClickLister. You also can override the getGap() to redefine the time between two clicks.

    import android.os.SystemClock;
    import android.view.View;
    
    /*****
     * Implement to fix the double click problem.
     * Avoid the fast double click for button and images.
     */
    public abstract class OnSingleClickListener implements View.OnClickListener {
        private long prevClickTime =0;
    
        @Override
        public void onClick(View v) {
            _onClick(v);
        }
    
        private synchronized void _onClick(View v){
            long current = SystemClock.elapsedRealtime();
            if(current-prevClickTime> getGap()){
                onSingleClick(v);
                prevClickTime = SystemClock.elapsedRealtime();
            }else {
                onDoubleClick(v);
                prevClickTime = 0;
            }
        }
    
        public abstract void onSingleClick(View v);
        public abstract void onDoubleClick(View v);
    
        /********
         *
         * @return The time in ms between two clicks.
         */
        public long getGap(){
            return 500L; //500ms
        }
    }
    
    0 讨论(0)
提交回复
热议问题