【问题标题】:Android Press LongClickListener get X, Y Coordinates, OnTouchListenerAndroid Press LongClickListener 获取X、Y坐标、OnTouchListener
【发布时间】:2019-07-04 18:54:47
【问题描述】:

我有一个按钮,想使用 LongClickListener,在更改按钮位置期间通过按下按钮获取坐标。如何在 LongClickListener 或其他方法中获取 Click/Mouse 的 X、Y 坐标。

我用 OnTouchListener 试了一下,效果很好。但问题是 TouchListener 会在每次点击时做出反应,而不是我想要的仅在按下时做出反应。

【问题讨论】:

    标签: android


    【解决方案1】:

    在 OnTouchListener 中这样做:

    OnTouchListener mOnTouch = new OnTouchListener()
    {
        @Override
        public boolean onTouch(View v, MotionEvent event)
        {            
           final int action = ev.getAction();
           switch (action & MotionEvent.ACTION_MASK) {
           case MotionEvent.ACTION_DOWN: {
              final int x = (int) ev.getX();
              final int y = (int) ev.getY();
           break;
        }
    };
    

    【讨论】:

    • 不,那不是我想要的。我不想每次点击都按下
    • 在 OnLongClickListener 中使用这些坐标
    • @Richard OnLongClickListener 在其回调中不提供坐标
    【解决方案2】:

    您必须将在 onTouch 中找到的最后已知坐标存储在某处(例如全局数据)并在您的 onLongClick 方法中读取它们。

    在某些情况下,您可能还必须使用 onInterceptTouchEvent

    【讨论】:

      【解决方案3】:

      解决办法是

      • 添加一个类变量来存储坐标
      • 使用OnTouchListener 保存 X、Y 坐标
      • 访问OnLongClickListener 中的 X,Y 坐标

      其他两个答案省略了一些可能有用的细节,所以这里有一个完整的演示:

      public class MainActivity extends AppCompatActivity {
      
          // class member variable to save the X,Y coordinates
          private float[] lastTouchDownXY = new float[2];
      
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
      
              // add both a touch listener and a long click listener
              View myView = findViewById(R.id.my_view);
              myView.setOnTouchListener(touchListener);
              myView.setOnLongClickListener(longClickListener);
          }
      
          // the purpose of the touch listener is just to store the touch X,Y coordinates
          View.OnTouchListener touchListener = new View.OnTouchListener() {
              @Override
              public boolean onTouch(View v, MotionEvent event) {
      
                  // save the X,Y coordinates
                  if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
                      lastTouchDownXY[0] = event.getX();
                      lastTouchDownXY[1] = event.getY();
                  }
      
                  // let the touch event pass on to whoever needs it
                  return false;
              }
          };
      
          View.OnLongClickListener longClickListener = new View.OnLongClickListener() {
              @Override
              public boolean onLongClick(View v) {
      
                  // retrieve the stored coordinates
                  float x = lastTouchDownXY[0];
                  float y = lastTouchDownXY[1];
      
                  // use the coordinates for whatever
                  Log.i("TAG", "onLongClick: x = " + x + ", y = " + y);
      
                  // we have consumed the touch event
                  return true;
              }
          };
      }
      

      【讨论】:

        【解决方案4】:
        @Override
        public boolean performLongClick(float x, float y) {
            super.performLongClick(x, y);
            doSomething();
            return super.performLongClick(x, y);
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-05-23
          • 2021-07-10
          • 2012-09-17
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多