开发者

Which Android Events do I use for this task?

开发者 https://www.devze.com 2023-03-27 11:16 出处:网络
I have a Java class that pertains to mouse listeners that I\'m wanting to convert over to my Android app but can\'t quite find the necessary events.

I have a Java class that pertains to mouse listeners that I'm wanting to convert over to my Android app but can't quite find the necessary events.

My Java app makes use of the following methods:

  • mouseClicked
  • mousePressed
  • mouseReleased

I'm wanting to do something similar however not with click events but touch events. I have come across OnTouchListener and did an override on the onTouch method.

What are the alternatives to mousePressed and mouseReleased开发者_高级运维?

Edit - (updated after Peter's response)

Are the following events correct:

  • ACTION_DOWN : mouseClicked
  • ACTION_MOVE : mousePressed
  • ACTION_UP : mouseReleased

EDIT - 2 Example Source

My Activity doesn't have any OnTouchListener at the moment because I was hoping I could keep all the touch logic in my View.

View:

/*Inside my View - Is it proper to do onTouch logic here? 
                   Or should I be doing this from the Activity?*/

public class myView {

    public boolean onTouch(MotionEvent event) {

        switch(event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                //draw arrow when screen is simply touched
                break;
            case MotionEvent.ACTION_MOVE:
                //Do Logic
                break;
            case MotionEvent.ACTION_UP:
                //Do Logic
                break;        

        }

    }
}

The reason I am doing the logic in my View is because I have some variables that I would like to grab directly rather than creating multiple extra get methods.

Am I able to do it like this? Or will I have to override the onTouch method in my Activity and do the logic there?


All touch events (down, up, move, multitouch) are handled via 'onTouch'. See this tutorial: http://www.zdnet.com/blog/burnette/how-to-use-multi-touch-in-android-2-part-3-understanding-touch-events/1775


If you want to register clicks on a View, implement and add an OnClickListener to it. When you want to register touch events you need to implement the OnTouchListener and add it to that View.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号