简体   繁体   中英

Continuous touch event in Android

I have 4 buttons on an activity. I have set Touch Listeners for all the buttons.

Button1.setOnTouchListener(this);
Button2.setOnTouchListener(this);
Button3.setOnTouchListener(this);
Button4.setOnTouchListener(this);

All I want to do is to get MotionEvent.ACTION_DOWN and MotionEvent.ACTION_UP when I am moving my finger over the buttons.

I am getting these events when I am touching INDIVIDUAL buttons, but not when I move my finger from one button to another.

In this case, first button should get ACTION_UP message and the next button should get ACTION_DOWN message.

Kindly help.

There is a certain logic in such behavior of touchevents.

I think the best decision for you will be to set OnTouchListener to parent view of Button1, Button2, etc or override its OnTouchEvent() method. After that you will be able to send MotionEvent to child views (buttons in your case) manually when it's necessary.

My initial instinct is that you are sending all of the touch events to the same function, causing some type of bottleneck.

What if you seperate the touch handlers into four seperate functions like this - that way, all buttons have independent touch handlers and there is no collission by having them all using the same function to handle their collective events. Make a seperate "onTouchEventListener" for each button and have it look like this:

Button1.setOnTouchListener(button1Listener);
Button2.setOnTouchListener(button2Listener);
Button3.setOnTouchListener(button3Listener);
Button4.setOnTouchListener(button4Listener);

Here is how to do it from the Android docs:

// Create an anonymous implementation of OnClickListener
private OnClickListener mCorkyListener = new OnClickListener() {
    public void onClick(View v) {
       // do something when the button is clicked
    }
};

protected void onCreate(Bundle savedValues) {
    ...
    // Capture our button from layout
    Button button = (Button)findViewById(R.id.corky);
    // Register the onClick listener with the implementation above
    button.setOnClickListener(mCorkyListener);
   ...
}

Since you're trying to capture a MotionEvent that involves all of the buttons, as opposed to just one, you will have to capture the MotionEvents in the parent view, then dispatch them to children buttons in the form of MotionEvent.ACTION_DOWN followed by MotionEvent.ACTION_UP.

I did something similar to make a multitouch keyboard out of a RelativeLayout containing many buttons. I basically overrided dispatchTouchEvent and kept track of each finger, sending ACITON_DOWN and ACTION_UP in place of ACTION_POINTER_DOWN and ACTION_POINTER_UP for secondary fingers to children.

It may be useful to look at the source of ViewGroup and see how it dispatches events to its children, and tweak the behavior to check when the finger goes out of child button bounds, and if so, send an ACTION_UP on the old button then an ACTION_DOWN on the new button.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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