有趣的android触摸问题

发布于 2024-12-15 13:33:37 字数 1586 浏览 4 评论 0原文

我遇到的问题是我正在使用 onTouch 事件的触摸监听器,当我只触摸一次时,触摸似乎被多次调用。 以下是我的代码

final TextView tv = (TextView)findViewById(R.id.TextView01);

        tv.setOnTouchListener(new View.OnTouchListener() {
            int count1 =0;
            int count2=0;
            public boolean onTouch(View arg0, MotionEvent event) {
                Log.d("Code777","Touch is being called finger");
                int i = event.getPointerCount();



                if(i==1 )
                {
                    if(count1==0)
                    {
                        Log.d("Code777","Touched with 1 finger");
                        count1++;
                        count2=0;
                    }

                }

                 if (i==2 )
                {
                     if(count2==0)
                     {
                        Log.d("Code777","Touched with 2 fingers");
                        edit.append(tv.getText());  
                        count2++;
                        count1=0;
                     }
                }
                return true;
            }
        });

我做错了什么吗? 对于单点触摸和双点触摸,它打印日志超过 3-4 次

问题更新的问题是,这两个事件现在都被触发

  if(event.getAction() == MotionEvent.ACTION_POINTER_2_DOWN)
                {
                    Log.d("Code777","2 finger touch");
                    return true;
                }else if(event.getAction() == MotionEvent.ACTION_DOWN)
                {
                        Log.d("Code777","Touched with 1 finger");


                    return true;
                }

The problem that i am having is I am using a touch listener for the onTouch event it seems that the touch is being called more than once when i just touch it once.
The following is my code

final TextView tv = (TextView)findViewById(R.id.TextView01);

        tv.setOnTouchListener(new View.OnTouchListener() {
            int count1 =0;
            int count2=0;
            public boolean onTouch(View arg0, MotionEvent event) {
                Log.d("Code777","Touch is being called finger");
                int i = event.getPointerCount();



                if(i==1 )
                {
                    if(count1==0)
                    {
                        Log.d("Code777","Touched with 1 finger");
                        count1++;
                        count2=0;
                    }

                }

                 if (i==2 )
                {
                     if(count2==0)
                     {
                        Log.d("Code777","Touched with 2 fingers");
                        edit.append(tv.getText());  
                        count2++;
                        count1=0;
                     }
                }
                return true;
            }
        });

Am i doing something wrong ??
It prints the log more than 3-4 times for both single touch and double touch

The problem updated problem is that both the events are getting fired now

  if(event.getAction() == MotionEvent.ACTION_POINTER_2_DOWN)
                {
                    Log.d("Code777","2 finger touch");
                    return true;
                }else if(event.getAction() == MotionEvent.ACTION_DOWN)
                {
                        Log.d("Code777","Touched with 1 finger");


                    return true;
                }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

椒妓 2024-12-22 13:33:37

您的代码将在每次触摸事件期间执行。第一次激活时,可能会执行 ACITON_DOWN 事件(当用户第一次触摸屏幕时)。第二次,可能是针对 ACTION_UP 事件(当用户从​​屏幕上抬起手指时)。同样,如果您在屏幕上滑动手指,对于 ACTION_MOVE 事件,相同的代码将会执行多次。您必须检查触摸的类型。做这样的事情:

switch(event.getAction()){
   case MotionEvent.ACTION_DOWN:
     // Do something that should only happen when the user first touches the screen
     break;
   case MotionEvent.ACTION_MOVE:
     // Do something that should only happen when the user is touching the screen
     break;
   case MotionEvent.ACTION_UP:
     // Do something that should only happen when the user stops touching the screen
     break;
}

编辑:

Android 中的多点触控充其量是奇怪的。并非所有设备都能处理它。并非所有设备都可以处理超过 x 次的触摸等。如果您想处理单个手指的向下情况,则可以使用 ACTION_POINTER_X 项。如果您要发生这样的一系列事件:

1.  User touches screen
2.  User touches screen with second finger
3.  User lifts finger 1
4.  User touches screen with finger 1
5.  User lifts finger 2
6.  User touches screen with finger 2
7.  User lifts both finger 1 and finger 2
8.  User touches screen with only finger 2
9.  User touches screen with finger 1

将被触发的事件将如下所示:

1.  ACTION_DOWN
2.  ACTION_POINTER_2_DOWN
3.  ACTION_POINTER_1_UP
4.  ACTION_POINTER_1_DOWN
5.  ACTION_POINTER_2_UP
6.  ACTION_POINTER_2_DOWN
7.  ACTION_UP (also, one of the pointers actions will fire depending on which finger was lifted first)
8.  ACTION_DOWN
9.  ACTION_POINTER_2_DOWN

等等。

You're code will execute during every touch event. The first time it activates, it's likely do to an ACITON_DOWN event (when the user first touches the screen). The second time, it is likely do to an ACTION_UP event (when the user lifts the finger from the screen). Likewise, if you were to swipe your finger around the screen, the same code will execute many times for an ACTION_MOVE event. You have to check for the types of touches that it is. Do something like this:

switch(event.getAction()){
   case MotionEvent.ACTION_DOWN:
     // Do something that should only happen when the user first touches the screen
     break;
   case MotionEvent.ACTION_MOVE:
     // Do something that should only happen when the user is touching the screen
     break;
   case MotionEvent.ACTION_UP:
     // Do something that should only happen when the user stops touching the screen
     break;
}

EDIT:

Multitouch in Android is odd at best. Not all devices can handle it. Not all device can handle more than x number of touches, etc. If you want to handle DOWN cases for individual fingers, then you can use the ACTION_POINTER_X items. If you were to have this series of events like so:

1.  User touches screen
2.  User touches screen with second finger
3.  User lifts finger 1
4.  User touches screen with finger 1
5.  User lifts finger 2
6.  User touches screen with finger 2
7.  User lifts both finger 1 and finger 2
8.  User touches screen with only finger 2
9.  User touches screen with finger 1

The events that will be fired will be like this:

1.  ACTION_DOWN
2.  ACTION_POINTER_2_DOWN
3.  ACTION_POINTER_1_UP
4.  ACTION_POINTER_1_DOWN
5.  ACTION_POINTER_2_UP
6.  ACTION_POINTER_2_DOWN
7.  ACTION_UP (also, one of the pointers actions will fire depending on which finger was lifted first)
8.  ACTION_DOWN
9.  ACTION_POINTER_2_DOWN

And so on.

江城子 2024-12-22 13:33:37

onTouchListener 通过 MotionEvents 向您发送多个操作。您可以使用 MotionEvent.getAction() 获取当前事件的操作。

您在这里注意到的很可能是您收到一个 ACTION_DOWN (手指已放在显示屏上) 事件,然后是一些小的 ACTION_MOVE 事件当您稍微移动手指时。最后您将得到 ACTION_UP (手指已抬起) 您可以通过忽略具有错误操作的事件来过滤掉这些内容。

例如,当发生移动事件时,仅从 onTouch() 返回。

public boolean onTouch(View arg0, MotionEvent event) {
    Log.d("Code777","Touch is being called finger");

    if(event.getAction() == MotionEvent.ACTION_MOVE) {
        Log.d("Code777", "Move event detected, ignored!");
        return false;
    }

    // Further processing ..
}

或使用开关来区分所有相关事件。

请参阅 MotionEvent 类文档以获取可能操作的列表。

An onTouchListener sends you multiple actions via MotionEvents. You can get the action for the current event with MotionEvent.getAction().

What you notice here is most likely that you get one ACTION_DOWN (a finger has been placed on the display) event followed by some small ACTION_MOVE events when you move the finger(s) slightly. In the end you will get ACTION_UP (a finger has been lifted) You can filter these out by ignoring events with the wrong action.

For example just return from onTouch() when a move event occured.

public boolean onTouch(View arg0, MotionEvent event) {
    Log.d("Code777","Touch is being called finger");

    if(event.getAction() == MotionEvent.ACTION_MOVE) {
        Log.d("Code777", "Move event detected, ignored!");
        return false;
    }

    // Further processing ..
}

or use a switch to differentiate between all relevant events.

See the MotionEvent class documentation for a list of possible actions.
.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文