【问题标题】:Detecting long presses in Android检测Android中的长按
【发布时间】:2018-08-17 07:37:05
【问题描述】:

我正在尝试检测 Android 中的长按。 GestureDetector 已弃用,因此我尝试使用 Handlers。但是handler 无法识别postDelayedremoveCallbacks。它是Cannot resolve method

    final Handler handler = new Handler() {
        @Override
        public void publish(LogRecord record) {

        }

        @Override
        public void flush() {

        }

        @Override
        public void close() throws SecurityException {

        }
    };

    Runnable longPressed = new Runnable() {
        @Override
        public void run() {
            Log.d("run", "long pressed");
        }
    };

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch(event.getAction()){
            case MotionEvent.ACTION_DOWN:
                handler.postDelayed(longPressed, 500);
                break;
            case MotionEvent.ACTION_MOVE:
                handler.removeCallbacks(longPressed);
                break;
            case MotionEvent.ACTION_UP:
                handler.removeCallbacks(longPressed);
                break;
        }

        return super.onTouchEvent(event);
    }
}

【问题讨论】:

标签: android touch-event android-handler long-press postdelayed


【解决方案1】:

View.OnLongClickListener.html 呢?


你会得到类似的东西:

yourView.setOnLongClickListener(new View.OnLongClickListener() {
  @Override public boolean onLongClick(View v) {
    // Toast it out!
    return false;
  }
});

【讨论】:

    【解决方案2】:

    GestureDetector 已弃用并不完全正确。
    只有那些不包括上下文作为构造函数参数被弃用。其他具有上下文的工作正常。

    final GestureDetector gestureDetector = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener() {
        public void onLongPress(MotionEvent e) {
            Log.e("", "Longpress detected");
        }
    });
    
    public boolean onTouchEvent(MotionEvent event) {
        return gestureDetector.onTouchEvent(event);
    };
    

    【讨论】:

      【解决方案3】:

      如果不想使用手势检测器,可以使用Handler。

      //Declare this flag globally
      boolean goneFlag = false;
      //Put this into the class
      final Handler handler = new Handler(); 
      Runnable mLongPressed = new Runnable() { 
          public void run() { 
              goneFlag = true;
              //Code for long click
          }   
      };
      
      //onTouch code
      @Override
      public boolean onTouch(View v, MotionEvent event) {
          switch (event.getAction()) {    
          case MotionEvent.ACTION_DOWN:
              handler.postDelayed(mLongPressed, 1000);
      
              break;
          case MotionEvent.ACTION_UP:
              handler.removeCallbacks(mLongPressed);
              if(Math.abs(event.getRawX() - initialTouchX) <= 2 && !goneFlag) {
                  //Code for single click
                  return false;
              }
              break;
          case MotionEvent.ACTION_MOVE:
              handler.removeCallbacks(mLongPressed);
      
              break;
          }
          return true;
      }
      

      【讨论】:

        猜你喜欢
        • 2011-12-16
        • 1970-01-01
        • 2016-04-04
        • 1970-01-01
        • 1970-01-01
        • 2011-06-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多