自定义button
@SuppressLint("AppCompatCustomView")
public class MyButton extends Button {
public MyButton(Context context) {
super(context);
}
public MyButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.e("1111111", "222===onTouchEvent=======ACTION_DOWN=========");
break;
case MotionEvent.ACTION_MOVE:
Log.e("1111111", "222====onTouchEvent=====ACTION_MOVE=========");
break;
case MotionEvent.ACTION_UP:
Log.e("1111111", "222====onTouchEvent=====ACTION_UP=========");
break;
}
return super.onTouchEvent(event);
}
}
布局:
<com.example.lenovo.myalbumtest.views.MyButton
android:id="@+id/btn_listen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="监听测试" />
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button btnListen;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnListen = ((Button) this.findViewById(R.id.btn_listen));
btnListen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e("1111111", "333=============onClick=========");
}
});
btnListen.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.e("1111111", "111===onTouch=======ACTION_DOWN=========");
break;
case MotionEvent.ACTION_MOVE:
Log.e("1111111", "111====onTouch======ACTION_MOVE=========");
break;
case MotionEvent.ACTION_UP:
Log.e("1111111", "111====onTouch====ACTION_UP=========");
break;
}
return false;
}
});
}
}
打印结果:
111===onTouch=======ACTION_DOWN=========
222===onTouchEvent=======ACTION_DOWN=========
111====onTouch======ACTION_MOVE=========
222====onTouchEvent=====ACTION_MOVE=========
111====onTouch======ACTION_MOVE=========
222====onTouchEvent=====ACTION_MOVE=========
111====onTouch======ACTION_MOVE=========
222====onTouchEvent=====ACTION_MOVE=========
111====onTouch======ACTION_MOVE=========
222====onTouchEvent=====ACTION_MOVE=========
111====onTouch====ACTION_UP=========
222====onTouchEvent=====ACTION_UP=========
333=============onClick=========
可见默认情况下:
1、优先级:onTouch > onTouchEvent > onClick
2、onTouch 默认返回 false
3、onTouchEvent 默认返回 super.onTouchEvent(event)
当只把onTouchEvent 直接返回true:
111===onTouch=======ACTION_DOWN=========
222===onTouchEvent=======ACTION_DOWN=========
111====onTouch======ACTION_MOVE=========
222====onTouchEvent=====ACTION_MOVE=========
111====onTouch======ACTION_MOVE=========
222====onTouchEvent=====ACTION_MOVE=========
111====onTouch======ACTION_MOVE=========
222====onTouchEvent=====ACTION_MOVE=========
111====onTouch====ACTION_UP=========
222====onTouchEvent=====ACTION_UP=========
可见只拦截了onClick事件。
当只把onTouchEvent 直接返回false:
111===onTouch=======ACTION_DOWN=========
222===onTouchEvent=======ACTION_DOWN=========
可见拦截了onClick事件,而且拦截了ACTION_MOVE、ACTION_UP事件
如果再把onTouchEvent改成只有一个break时:
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.e("1111111", "222===onTouchEvent=======ACTION_DOWN=========");
// break;
case MotionEvent.ACTION_MOVE:
Log.e("1111111", "222====onTouchEvent=====ACTION_MOVE=========");
// break;
case MotionEvent.ACTION_UP:
Log.e("1111111", "222====onTouchEvent=====ACTION_UP=========");
break;
}
return false;
}
打印:
111===onTouch=======ACTION_DOWN=========
222===onTouchEvent=======ACTION_DOWN=========
222====onTouchEvent=====ACTION_MOVE=========
222====onTouchEvent=====ACTION_UP=========
结论:onTouchEvent返回false,只走一次onTouch、onTouchEvent,拦截了onClick
在case MotionEvent.ACTION_DOWN时return true的问题:return true
表示当前控件已经消费了下按动作,之后的ACTION_MOVE、ACTION_UP动作也会继续传递到当前控件中;
如果我们在case MotionEvent.ACTION_DOWN时return false,
那么后序的ACTION_MOVE、ACTION_UP动作就不会再传到这个控件来了。
当只把onTouch返回true:
111===onTouch=======ACTION_DOWN=========
111====onTouch======ACTION_MOVE=========
111====onTouch======ACTION_MOVE=========
111====onTouch======ACTION_MOVE=========
111====onTouch======ACTION_MOVE=========
111====onTouch======ACTION_MOVE=========
111====onTouch====ACTION_UP=========
结论:onTouch返回true,拦截onClick、onTouchEvent。
另外:当onTouch返回true,onTouchEvent再设置返回false时没效果。
打印log如下:
111===onTouch=======ACTION_DOWN=========
111====onTouch======ACTION_MOVE=========
111====onTouch======ACTION_MOVE=========
111====onTouch======ACTION_MOVE=========
111====onTouch======ACTION_MOVE=========
111====onTouch======ACTION_MOVE=========
111====onTouch======ACTION_MOVE=========
111====onTouch======ACTION_MOVE=========
111====onTouch====ACTION_UP=========