Using the Ontouchevent method of activity to realize the monitoring of fingers sliding up and down
Using the activity of the Ontouchevent method to monitor the finger click events, the fingers will slide when the first press, slide down another place to lift, we can according to the coordinates and the lifting coordinates to calculate the user is in which direction to slide.
Package com.example.testtt;
Import android.app.Activity;
Import Android.os.Bundle;
Import android.view.MotionEvent;
Import Android.widget.Toast;
public class Mainactivity extends activity {//finger pressed point for (x1, y1) finger left screen for (x2, y2) float x1 = 0;
float x2 = 0;
float y1 = 0;
float y2 = 0;
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main); @Override public boolean ontouchevent (Motionevent event) {//Inherits the Ontouchevent method of the activity, directly listening to the Click event if (event.getactio
N () = = Motionevent.action_down) {///when the finger is pressed x1 = Event.getx ();
Y1 = Event.gety ();
if (event.getaction () = = motionevent.action_up) {//When the finger leaves x2 = event.getx ();
y2 = Event.gety ();
if (Y1-y2 >) {toast.maketext (mainactivity.this, "Up slippery", toast.length_short). Show ();
else if (Y2-y1 >) {toast.maketext (mainactivity.this, "downward", Toast.length_short). Show (); else if (X1-x2 >) {toast.maketext (M)Ainactivity.this, "slide to the left", Toast.length_short. Show ();
else if (X2-x1 >) {toast.maketext (mainactivity.this, slide right, Toast.length_short). Show ();
} return Super.ontouchevent (event);
}
}