Android 事件触发机制的深入学习
Android事件触发机制的深入学习
最近在研究android的事件触发和传播机制,说来很惭愧,web下的事件太熟悉不过了,可在android中却很郁闷,常用的触摸事件都糊里糊涂的,在网上看了半天,也整理一份,供大家参考:
publicclassCustomDefaultActivityimplementsOnTouchListener{ privateGestureDetectorgd1; privateTableLayouttableLayout; @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.custom_default); tableLayout=(TableLayout)findViewById(R.id.tableLayout); gd1=newGestureDetector(CustomDefaultActivity.this,newMyGesture()); tableLayout.setOnTouchListener(this); tableLayout.setLongClickable(true); } @Override publicbooleandispatchTouchEvent(MotionEventevent){ Log.e("-----------------------------------------------------------------------", "dispatchTouchEvent"); if(gd1.onTouchEvent(event)){ event.setAction(MotionEvent.ACTION_CANCEL); } returnsuper.dispatchTouchEvent(event); } @Override publicbooleanonTouch(Viewarg0,MotionEventarg1){ Log.e("-------","onTouch"); returngd1.onTouchEvent(arg1); } publicclassMyGestureimplementsOnGestureListener{ @Override publicbooleanonDown(MotionEventarg0){ Log.e("-------", "onDown,按下时触发["+arg0.getX()+","+arg0.getY()+"]"); returntrue; } @Override publicvoidonLongPress(MotionEventarg0){ Log.e("-------","onLongPress,长时间按住时触发"); } @Override publicbooleanonScroll(MotionEventarg0,MotionEventarg1,floatarg2, floatarg3){ Log.e("-------", "onScroll,滑动拖动事件["+arg0.getX()+","+arg0.getY()+"]=["+ arg1.getX()+","+arg1.getY()+"]"); returntrue; } @Override publicbooleanonFling(MotionEventarg0,MotionEventarg1,floatarg2, floatarg3){ Log.e("-------","onFling,滑动甩出事件"); returntrue; } @Override publicvoidonShowPress(MotionEventarg0){ Log.e("-------","onShowPress,down事件发生,而move或up还没发生前触发该事件"); } @Override publicbooleanonSingleTapUp(MotionEventarg0){ Log.e("-------","onSingleTapUp释放事件"); returntrue; } } }
监控触摸事件,主要是实现OnGestureListener类,并重写其中的几个方法,以监听事件发生。
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!