标签:
btn =(Button) findViewById(R.id.send); btn.setOnClickListener(newOnClickListener(){ @Override publicvoid onClick(View v){ Log.d("TAG","onClick execute"); } }); btn.setOnTouchListener(newOnTouchListener(){ @Override publicboolean onTouch(View v,MotionEvent event){ Log.d("TAG","onTouch execute, action "+ event.getAction()); returnfalse; } });
任何控件都会调用到dispatchTouchEvent事件分发,本来没有这个方法就往父类找。
publicboolean dispatchTouchEvent(MotionEvent event){ if(mOnTouchListener !=null&&(mViewFlags & ENABLED_MASK)== ENABLED && mOnTouchListener.onTouch(this, event)){ return true; } return onTouchEvent(event); }
publicvoid setOnTouchListener(OnTouchListener l){ mOnTouchListener = l; }
publicboolean performClick(){ sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED); if(mOnClickListener !=null){ playSoundEffect(SoundEffectConstants.CLICK); mOnClickListener.onClick(this); returntrue; } returnfalse; }
publicvoid setOnClickListener(OnClickListener l){ if(!isClickable()){ setClickable(true); } mOnClickListener = l; }
publicclassMyLayoutextendsLinearLayout{ publicMyLayout(Context context,AttributeSet attrs){ super(context, attrs); } @Override publicboolean onInterceptTouchEvent(MotionEvent ev){ return true; } }
myLayout =(LinearLayout) findViewById(R.id.my_layout); button1 =(Button) findViewById(R.id.button1); button2 =(Button) findViewById(R.id.button2); myLayout.setOnTouchListener(newOnTouchListener(){ @Override publicboolean onTouch(View v,MotionEvent event){ Log.d("TAG","myLayout on touch"); return false; } }); button1.setOnClickListener(newOnClickListener(){ @Override publicvoid onClick(View v){ Log.d("TAG","You clicked button1"); } }); button2.setOnClickListener(newOnClickListener(){ @Override publicvoid onClick(View v){ Log.d("TAG","You clicked button2"); } });
if(disallowIntercept ||!onInterceptTouchEvent(ev)){ ev.setAction(MotionEvent.ACTION_DOWN); finalint scrolledXInt =(int) scrolledXFloat; finalint scrolledYInt =(int) scrolledYFloat; finalView[] children = mChildren; finalint count = mChildrenCount;
if(target ==null){ ev.setLocation(xf, yf); if((mPrivateFlags & CANCEL_NEXT_UP_EVENT)!=0){ ev.setAction(MotionEvent.ACTION_CANCEL); mPrivateFlags &=~CANCEL_NEXT_UP_EVENT; } return super.dispatchTouchEvent(ev); }
child.mPrivateFlags &=~CANCEL_NEXT_UP_EVENT; if(child.dispatchTouchEvent(ev)){ mMotionTarget = child; returntrue; }
标签:
原文地址:http://www.cnblogs.com/fruitbolgs/p/4291123.html