ontouch-控件添加ontouch监听事件
1,代码public class CalculatorViewPager extends ViewPager {}中
package com.android.calculator2; import android.content.Context; import android.support.v4.view.ViewPager; import android.util.AttributeSet; import android.view.MotionEvent; public class CalculatorViewPager extends ViewPager { private boolean enabled; public CalculatorViewPager(Context context) { super(context); this.enabled = false; } public CalculatorViewPager(Context context, AttributeSet attrs) { super(context, attrs); this.enabled = false; } public boolean shouldDelayChildPressedState() { return false; } @Override public boolean onTouchEvent(MotionEvent event) { if (this.enabled) { return super.onTouchEvent(event); } android.util.Log.e("zhangshuli", "pantouch"); return false; } @Override public boolean onInterceptTouchEvent(MotionEvent event) { if (this.enabled) { return super.onInterceptTouchEvent(event); } android.util.Log.e("zhangshuli", "paninter"); return false; } public void setPagingEnabled(boolean enabled) { this.enabled = enabled; } }
打印结果
01-01 10:01:49.274 E/zhangshuli( 4832): paninter 01-01 10:01:49.292 E/zhangshuli( 4832): paninter 01-01 10:01:49.308 E/zhangshuli( 4832): paninter 01-01 10:01:49.325 E/zhangshuli( 4832): paninter 01-01 10:01:49.342 E/zhangshuli( 4832): paninter 01-01 10:01:49.360 E/zhangshuli( 4832): paninter 01-01 10:01:49.376 E/zhangshuli( 4832): paninter 01-01 10:01:49.393 E/zhangshuli( 4832): paninter 01-01 10:01:49.411 E/zhangshuli( 4832): paninter 01-01 10:01:49.428 E/zhangshuli( 4832): paninter 01-01 10:01:49.445 E/zhangshuli( 4832): paninter 01-01 10:01:49.464 E/zhangshuli( 4832): paninter 01-01 10:01:49.469 E/zhangshuli( 4832): paninter
touch 没有执行,点击按钮有效果
1111
@Override public boolean onTouchEvent(MotionEvent event) { android.util.Log.e("zhangshuli", "pannel"); return false; } @Override public boolean onInterceptTouchEvent(MotionEvent event) { android.util.Log.e("zhangshuli", "inter"); return true; }
结果
01-01 09:36:56.131 E/zhangshuli( 4007): inter 01-01 09:36:56.132 E/zhangshuli( 4007): pannel 01-01 09:36:57.369 E/zhangshuli( 4007): inter 01-01 09:36:57.369 E/zhangshuli( 4007): pannel 01-01 09:36:58.254 E/zhangshuli( 4007): inter 01-01 09:36:58.255 E/zhangshuli( 4007): pannel 01-01 09:36:59.125 E/zhangshuli( 4007): inter 01-01 09:36:59.125 E/zhangshuli( 4007): pannel
都执行,点击没有效果
2.代码
public boolean onTouchEvent(MotionEvent event) { android.util.Log.e("zhangshuli", "pannel"); return true; } @Override public boolean onInterceptTouchEvent(MotionEvent event) { android.util.Log.e("zhangshuli", "inter"); return false; }
打印如下
01-01 09:33:52.885 E/zhangshuli( 3892): inter 01-01 09:33:52.902 E/zhangshuli( 3892): inter 01-01 09:33:52.919 E/zhangshuli( 3892): inter 01-01 09:33:52.936 E/zhangshuli( 3892): inter 01-01 09:33:52.955 E/zhangshuli( 3892): inter 01-01 09:33:52.971 E/zhangshuli( 3892): inter 01-01 09:33:52.988 E/zhangshuli( 3892): inter 01-01 09:33:53.005 E/zhangshuli( 3892): inter 01-01 09:33:53.007 E/zhangshuli( 3892): inter
点击有效果
3.代码
@Override public boolean onTouchEvent(MotionEvent event) { if (this.enabled) { return super.onTouchEvent(event); } android.util.Log.e("zhangshuli", "pannel"); return true; } @Override public boolean onInterceptTouchEvent(MotionEvent event) { if (this.enabled) { return super.onInterceptTouchEvent(event); } android.util.Log.e("zhangshuli", "inter"); return true; }
打印
01-01 09:42:59.198 E/zhangshuli( 4301): inter 01-01 09:42:59.198 E/zhangshuli( 4301): pannel 01-01 09:42:59.225 E/zhangshuli( 4301): pannel 01-01 09:42:59.241 E/zhangshuli( 4301): pannel 01-01 09:42:59.258 E/zhangshuli( 4301): pannel 01-01 09:42:59.275 E/zhangshuli( 4301): pannel 01-01 09:42:59.292 E/zhangshuli( 4301): pannel 01-01 09:42:59.309 E/zhangshuli( 4301): pannel 01-01 09:42:59.326 E/zhangshuli( 4301): pannel 01-01 09:42:59.343 E/zhangshuli( 4301): pannel 01-01 09:42:59.362 E/zhangshuli( 4301): pannel 01-01 09:42:59.377 E/zhangshuli( 4301): pannel 01-01 09:42:59.395 E/zhangshuli( 4301): pannel 01-01 09:42:59.412 E/zhangshuli( 4301): pannel 01-01 09:42:59.437 E/zhangshuli( 4301): pannel 01-01 09:42:59.447 E/zhangshuli( 4301): pannel 01-01 09:42:59.463 E/zhangshuli( 4301): pannel 01-01 09:42:59.480 E/zhangshuli( 4301): pannel 01-01 09:42:59.497 E/zhangshuli( 4301): pannel 01-01 09:42:59.514 E/zhangshuli( 4301): pannel 01-01 09:42:59.532 E/zhangshuli( 4301): pannel 01-01 09:42:59.537 E/zhangshuli( 4301): pannel 01-01 09:43:01.267 E/zhangshuli( 4301): inter
点击按钮效果
text.setontouchlisten
那么只有在text这个控件内点击才有效果