After many experiments with onTouchEvent and onTouch, I found that onTouch works everywhere you want (whether it is in activity or view) as long as you have declared the interface and put the Listener right! On the other hand, onTouchEvent only works inside a View! Is my assumption correct? Is this the real difference?
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
I found another difference. onTouchEvent does not seem to get the deprecated events MotionEvent.ACTION_POINTER_2_DOWN and MotionEvent.ACTION_POINTER_2_UP.
Of course they are quite old and we shouldn't be using them.
This is in Android 5.1 api 22.
onTouchEvent is a method implemented by the View, Activity and other base classes like LinearLayout, etc..
you can override this method by any derived classes
whereas
onTouch() is defined by the interface OnTouchListener{}
public interface OnTouchListener { boolean onTouch(View var1, MotionEvent var2); }
so you only need to implement one when setting this interface to a class
Yes you are correct -
onTouch()
is used by users of theView
to get touch events whileonTouchEvent()
is used by derived classes of theView
to get touch events.The onTouchEvent() actually will get called by the Activity if none of the views consume the touch event.
And as you say, the onTouch() can be used in any class, as long as:
view.setOnTouchListener(foo);
I have used ontouch() and ontouchevent(), as ontouch is used when i want to work on elements of single view, like buttons , imagebuttons etc on single view (say Linearlayout ), whereas when i want to work on areas rest of the my elements(e.g button) i use ontouchevent.
While creating a custom View ,you can
@override onTouchEvent(MotionEvent e){}
whereas you can add onTouch to any View,ViewGroup or Activity.
onTouch(View v, MotionEvent e) { //you can filter any View's touch }
onTouch is generic and onTouchEvent is specific to View. And you can also filter your view using onTouch.