简体   繁体   English

使用MotionEvent(ACTION_MOVE)

[英]Using of MotionEvent (ACTION_MOVE)

So I have special question to you: How can I catch buttons when I use move action? 因此,我有一个特别的问题:使用移动动作时如何捕捉按钮? How can I handle moves on my screen for buttons(or other elements)? 如何处理屏幕上按钮(或其他元素)的移动?

I used MotionEvent (ACTION_MOVE) but by using that fragment of code i don't get the desired result 我使用了MotionEvent(ACTION_MOVE),但是使用该代码片段却无法获得理想的结果

in OnCreate 在OnCreate中

btn1.setOnTouchListener(this); //for all of buttons on Activity

in onTouch 在onTouch中

switch(event.getAction()) { 
  case MotionEvent.ACTION_MOVE: 
       //actions
  break;
}

actions will be occur for first button from which the movement starts. 从移动开始的第一个按钮将发生动作。 In other case nothing will occur 在其他情况下,将不会发生任何事情

At this time I think that I can use ACTION_MOVE for my Activity not for each button or other elements and save coordinates of buttons(top left and bottom right) to arraylist. 目前,我认为可以将ACTION_MOVE用于我的Activity,而不是用于每个按钮或其他元素,并将按钮的坐标(左上和右下)保存到arraylist。 And when movement starts I could compare this coordinates and real coordinates of movement. 当运动开始时,我可以比较该坐标和运动的真实坐标。 So by that way I could know on which buttons movement was. 因此,通过这种方式,我可以知道运动在哪个按钮上。

Probably I reinvent the wheel. 可能是我重新发明了轮子。 That why I ask for your help) 那就是为什么我要你的帮助)

Would you explain more clear? 您能解释得更清楚吗? From what I see you need probably do two things: First add listener to all buttons and let your activity implement onTouchListener. 从我看来,您可能需要做两件事:首先将侦听器添加到所有按钮,然后让您的活动实现onTouchListener。
Second you need to save initial coordinate for each button in MotionEvent.ACTION_DOWN(if I'm right about syntax). 其次,您需要为MotionEvent.ACTION_DOWN中的每个按钮保存初始坐标(如果我对语法正确的话)。
This way you will have access to all buttons of yours and you can save their coordinates correctly 这样,您将可以访问自己所有的按钮,并且可以正确保存它们的坐标

You will get the view inside the touch listener, using which you can identify. 您将在触摸侦听器中获得视图,您可以使用该视图进行识别。

Button btn1 = (Button) findViewById(R.id.btn1);
Button btn2 = (Button) findViewById(R.id.btn2);
Button btn3 = (Button) findViewById(R.id.btn3);

btn1.setOnTouchListener(touchListener);
btn2.setOnTouchListener(touchListener);
btn3.setOnTouchListener(touchListener);

private OnTouchListener touchListener = new OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_MOVE) {
            switch (v.getId()) {
            case R.id.btn1:
                //Button 1
                break;

            case R.id.btn2:
                //Button 2
                break;

            case R.id.btn3:
                //Button 3
                break;
            }
        }
        return false;
    }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM