Xamarin.Android实现手势监听方法

本文介绍了如何在Xamarin.Android环境下,利用C#语言和GestureDetector类来实现手势监听。通过创建继承自Activity和GestureDetector.IOnGestureListener的Activity1类,并在OnCreate函数中设置手势检测,实现Android应用的手势事件处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文叙述的是使用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){}

 



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值