开发者

Handling UI Events in Android

开发者 https://www.devze.com 2023-01-06 07:47 出处:网络
I\'m trying to modify some code I found online to my needs. It is supposed to catch a MotionEvent (a fling in particular) and launch a separate activity. I am new to java, so I\'m having some trouble

I'm trying to modify some code I found online to my needs. It is supposed to catch a MotionEvent (a fling in particular) and launch a separate activity. I am new to java, so I'm having some trouble understanding the code. Here's what I have so far:

public class Hypertension extends Activity {
 private GestureDetector flingDetector;
 View.OnTouchListener gestureListener;
 private TextView redView;
 private TextView greenView;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        redView = (TextView)findViewById(R.id.buttonRed);
        greenView = (TextView)findViewById(R.id.buttonGreen);

        redView.setOnTouchListener(gestureListener);
        greenView.setOnTouchListener(gestureListener);

        flingDetector = new GestureDetector(new MyFlingListener());
        gestureListener = new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                if (flingDetector.onTouchEvent(event)) {
                    //startActivity(new Intent(Hypertension.this, Green.class));
                 return true;
                }
                return false;
            }
        };
    }

    class MyFlingListener extends SimpleOnGestureL开发者_C百科istener {
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
          float velocityY) {
         // TODO Auto-generated method stub
         startActivity(new Intent(Hypertension.this, Green.class));
         return false;
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (flingDetector.onTouchEvent(event))
         return true;
     else
      return false;
    }
}

My understanding is that when the screen is touched, onTouchEvent is called. In the above code, this method is overridden to call flingDetector, which listens for a fling. If one is heard, it launches the activity.

As of now, however, nothing happens when I perform a fling in the emulator. Also, I am fairly confused as to what the return values of the various boolean methods actually represent.


You have two onTouchEvent methods in your code. One is in the GestureDetector class (not overridden), and the other is in your Hypertension activity class (which you have overridden at the bottom).

When someone triggers the TouchEvent in the main activity you explicitly calling the GestureDetector one (passing down the event) here:

if (flingDetector.onTouchEvent(event)) return true;

But if you haven't overridden the onTouchEvent method of that class then nothing is going to happen!


The purpose of overriding these "onSomething()" methods is so that they will get called automatically when an event triggers. In general the way to work with listeners is as follows:

  1. Create a subclass of the Listener class for the event and override its "onEvent()" method to do something when the event is triggered
  2. Call the "setListener( Listener)" method of the object you want to trigger said events after it has been initialized--passing in your previously created Listener
  3. Sit back and watch :)


For all event listeners that return a boolean it should return true when it handles the event, so in your example if the flingDetector handles the onTouchEvent it returns true.

This question has been posed before and there are some great answers there.

0

精彩评论

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