Set Duration of Long Key press Listener

前端 未结 3 2001
我在风中等你
我在风中等你 2021-01-23 11:33

Can we set duration for Long key press listener? What i want is, if user keeps touching the screen for 3 sec then my long key press listener should trigger and open my pop up fo

3条回答
  •  礼貌的吻别
    2021-01-23 12:02

    Override onTouch Listener ,then handle pressed,released event and set timer during button pressed (event == "pressed")

    private Timer timer;
    
     public LongClickTimer(int seconds) {
                timer = new Timer();
                timer.schedule(new LongClickTask(), seconds *1000);         
            }
     class LongClickTask extends TimerTask {
                public void run() { 
                 // do what you want            
                    timer.cancel(); 
                }
            }
         button.setOnTouchListener(new OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    Log.v(TAG, "EVENT" + event.toString());
                        if(event.getAction == 2) {// pressed                        
                        new LongClickTimer(5); // schedule for 5 seconds
                        }else{          
                         timer.cancel();
                        }
                   return false;
                  }
               });
    

提交回复
热议问题