<small id='bLITL'></small><noframes id='bLITL'>

    1. <i id='bLITL'><tr id='bLITL'><dt id='bLITL'><q id='bLITL'><span id='bLITL'><b id='bLITL'><form id='bLITL'><ins id='bLITL'></ins><ul id='bLITL'></ul><sub id='bLITL'></sub></form><legend id='bLITL'></legend><bdo id='bLITL'><pre id='bLITL'><center id='bLITL'></center></pre></bdo></b><th id='bLITL'></th></span></q></dt></tr></i><div id='bLITL'><tfoot id='bLITL'></tfoot><dl id='bLITL'><fieldset id='bLITL'></fieldset></dl></div>
      <legend id='bLITL'><style id='bLITL'><dir id='bLITL'><q id='bLITL'></q></dir></style></legend>

        • <bdo id='bLITL'></bdo><ul id='bLITL'></ul>
      1. <tfoot id='bLITL'></tfoot>
      2. onTouchEvent()中如何区分移动和点击?

        时间:2023-08-31

        <small id='h04gM'></small><noframes id='h04gM'>

          1. <i id='h04gM'><tr id='h04gM'><dt id='h04gM'><q id='h04gM'><span id='h04gM'><b id='h04gM'><form id='h04gM'><ins id='h04gM'></ins><ul id='h04gM'></ul><sub id='h04gM'></sub></form><legend id='h04gM'></legend><bdo id='h04gM'><pre id='h04gM'><center id='h04gM'></center></pre></bdo></b><th id='h04gM'></th></span></q></dt></tr></i><div id='h04gM'><tfoot id='h04gM'></tfoot><dl id='h04gM'><fieldset id='h04gM'></fieldset></dl></div>
            <legend id='h04gM'><style id='h04gM'><dir id='h04gM'><q id='h04gM'></q></dir></style></legend>

            1. <tfoot id='h04gM'></tfoot>
                <tbody id='h04gM'></tbody>
                • <bdo id='h04gM'></bdo><ul id='h04gM'></ul>
                  本文介绍了onTouchEvent()中如何区分移动和点击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  在我的应用程序中,我需要同时处理移动和点击事件.

                  In my application, I need to handle both move and click events.

                  点击是一个 ACTION_DOWN 动作、几个 ACTION_MOVE 动作和一个 ACTION_UP 动作的序列.理论上,如果您收到一个 ACTION_DOWN 事件,然后是一个 ACTION_UP 事件 - 这意味着用户刚刚单击了您的视图.

                  A click is a sequence of one ACTION_DOWN action, several ACTION_MOVE actions and one ACTION_UP action. In theory, if you get an ACTION_DOWN event and then an ACTION_UP event - it means that the user has just clicked your View.

                  但实际上,此序列不适用于某些设备.在我的三星 Galaxy Gio 上,只需单击我的视图就会得到这样的序列:ACTION_DOWN,几次 ACTION_MOVE,然后是 ACTION_UP.IE.我收到一些带有 ACTION_MOVE 操作代码的意外 OnTouchEvent 触发.我从来没有(或几乎从来没有)得到序列 ACTION_DOWN -> ACTION_UP.

                  But in practice, this sequence doesn't work on some devices. On my Samsung Galaxy Gio I get such sequences when just clicking my View: ACTION_DOWN, several times ACTION_MOVE, then ACTION_UP. I.e. I get some unexpectable OnTouchEvent firings with ACTION_MOVE action code. I never (or almost never) get sequence ACTION_DOWN -> ACTION_UP.

                  我也不能使用 OnClickListener,因为它没有给出点击的位置.那么如何检测点击事件并将其与移动区别开来呢?

                  I also cannot use OnClickListener because it does not gives the position of the click. So how can I detect click event and differ it from move?

                  推荐答案

                  这是另一个非常简单的解决方案,不需要您担心手指被移动.如果您将点击作为简单移动距离的基础,那么您如何区分点击和长点击.

                  Here's another solution that is very simple and doesn't require you to worry about the finger being moved. If you are basing a click as simply the distance moved then how can you differentiate a click and a long click.

                  您可以在其中添加更多智能并包括移动的距离,但我还没有遇到一个实例,即用户可以在 200 毫秒内移动的距离应该构成移动而不是点击.

                  You could put more smarts into this and include the distance moved, but i'm yet to come across an instance when the distance a user can move in 200 milliseconds should constitute a move as opposed to a click.

                  setOnTouchListener(new OnTouchListener() {
                      private static final int MAX_CLICK_DURATION = 200;
                      private long startClickTime;
                  
                      @Override
                      public boolean onTouch(View v, MotionEvent event) {
                          switch (event.getAction()) {
                              case MotionEvent.ACTION_DOWN: {
                                  startClickTime = Calendar.getInstance().getTimeInMillis();
                                  break;
                              }
                              case MotionEvent.ACTION_UP: {
                                  long clickDuration = Calendar.getInstance().getTimeInMillis() - startClickTime;
                                  if(clickDuration < MAX_CLICK_DURATION) {
                                      //click event has occurred
                                  }
                              }
                          }
                          return true;
                      }
                  });
                  

                  这篇关于onTouchEvent()中如何区分移动和点击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Android 应用上的自定义事件监听器 下一篇:LongClick 事件发生得太快.如何增加触发它所需的点

                  相关文章

                  最新文章

                    <i id='sDqPc'><tr id='sDqPc'><dt id='sDqPc'><q id='sDqPc'><span id='sDqPc'><b id='sDqPc'><form id='sDqPc'><ins id='sDqPc'></ins><ul id='sDqPc'></ul><sub id='sDqPc'></sub></form><legend id='sDqPc'></legend><bdo id='sDqPc'><pre id='sDqPc'><center id='sDqPc'></center></pre></bdo></b><th id='sDqPc'></th></span></q></dt></tr></i><div id='sDqPc'><tfoot id='sDqPc'></tfoot><dl id='sDqPc'><fieldset id='sDqPc'></fieldset></dl></div>
                  1. <small id='sDqPc'></small><noframes id='sDqPc'>

                  2. <tfoot id='sDqPc'></tfoot>
                      <bdo id='sDqPc'></bdo><ul id='sDqPc'></ul>

                      <legend id='sDqPc'><style id='sDqPc'><dir id='sDqPc'><q id='sDqPc'></q></dir></style></legend>