本文叙述的是使用C#语言编写Android应用的方法。
手势监听,依靠的是GestureDetector类,与Java中相同。
1. 定义类Activity1,同时继承Activity类和抽象接口类GestureDetector.IOnGestureListener。
public class Activity1 : Activity, GestureDetector.IOnGestureListener
2. 在类中定义GestureDetector类的一个对象。
private GestureDetector _gestureDetector;
3. OnCreate函数不需要修改,只需要为其赋值。
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
_gestureDetector = new GestureDetector(this);
}
4. 在类Activity1中,重写OnTouchEvent函数,添加手势监听。 public override bool OnTouchEvent(MotionEvent e)
{
_gestureDetector.OnTouchEvent(e);
return false;
}
5.在类Activity1中,定义事件处理函数,处理手势。
public bool OnDown(MotionEvent e){}
public bool OnFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY){}
public void OnLongPress(MotionEvent e) {}
public bool OnScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY){}
public void OnShowPress(MotionEvent e) {}
public bool OnSingleTapUp(MotionEvent e){}