正常例如原生的button都是给做好了的,但是由于项目需求,自定义了一个button重写了他的触摸事件后就没有这个处理了。需要自己重写onTouchEvent事件。解决如下:
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
float touchX = event.getX();
float touchY = event.getY();
float maxX = getWidth();
float maxY = getHeight();
if (touchX < 0 || touchX > maxX || touchY < 0 || touchY > maxY) { //触摸区域在Button外
this.setPressed(false); //反应色
return false; //不走up事件
}
if (!this.isEnabled() && null != this.onDisableClickListener) {
this.onDisableClickListener.onClick(this);
Log.d("ljx", "the button is touched success");
}
}
return super.onTouchEvent(event);
}