Android 事件触发机制的深入学习
程序员文章站
2023-12-01 18:25:22
android 事件触发机制的深入学习
最近在研究android的事件触发和传播机制,说来很惭愧,web下的事件太熟悉不过了,可在android中却很郁闷,常...
android 事件触发机制的深入学习
最近在研究android的事件触发和传播机制,说来很惭愧,web下的事件太熟悉不过了,可在android中却很郁闷,常用的触摸事件都糊里糊涂的,在网上看了半天,也整理一份,供大家参考:
public class customdefaultactivity implements ontouchlistener { private gesturedetector gd1; private tablelayout tablelayout; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.custom_default); tablelayout = (tablelayout) findviewbyid(r.id.tablelayout); gd1 = new gesturedetector(customdefaultactivity.this, new mygesture()); tablelayout.setontouchlistener(this); tablelayout.setlongclickable(true); } @override public boolean dispatchtouchevent(motionevent event) { log.e("-----------------------------------------------------------------------", "dispatchtouchevent"); if (gd1.ontouchevent(event)) { event.setaction(motionevent.action_cancel); } return super.dispatchtouchevent(event); } @override public boolean ontouch(view arg0, motionevent arg1) { log.e("-------", "ontouch"); return gd1.ontouchevent(arg1); } public class mygesture implements ongesturelistener { @override public boolean ondown(motionevent arg0) { log.e("-------", "ondown, 按下时触发[" + arg0.getx() + "," + arg0.gety() + "]"); return true; } @override public void onlongpress(motionevent arg0) { log.e("-------", "onlongpress,长时间按住时触发"); } @override public boolean onscroll(motionevent arg0, motionevent arg1, float arg2, float arg3) { log.e("-------", "onscroll,滑动拖动事件[" + arg0.getx() + "," + arg0.gety() + "]=[" + arg1.getx() + "," + arg1.gety() + "]"); return true; } @override public boolean onfling(motionevent arg0, motionevent arg1, float arg2, float arg3) { log.e("-------", "onfling, 滑动甩出事件"); return true; } @override public void onshowpress(motionevent arg0) { log.e("-------", "onshowpress,down事件发生,而move或up还没发生前触发该事件"); } @override public boolean onsingletapup(motionevent arg0) { log.e("-------", "onsingletapup 释放事件"); return true; } } }
监控触摸事件,主要是实现ongesturelistener类,并重写其中的几个方法,以监听事件发生。
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!