Android 设置整个页面不能点击
在做这个需求的时候,看到网上的那些博客,基本都是说:
1、设置根布局(LinearLayout) LinearLayout.setOnclickListener(null);
2、设置根布局(LinearLayout) LinearLayout.setEnable(false)…等.
通过实践验证了上述2个方案。并没有达到控制整个页面都不能点击的效果。故自己通过拦截ViewGroup的Touch事件进行动态设置。自测是可以达到效果。
public class EnableLinearLayout extends LinearLayout {
boolean isNoClick = false;
/***
* 设置是否可以点击
* @param isNoClick true:不能点击 false:可以点击
*/
public void setNoClick(boolean isNoClick) {
this.isNoClick = isNoClick;
}
public EnableLinearLayout(Context context) {
super(context);
}
public EnableLinearLayout(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
if (isNoClick) {
return true;
}
return super.dispatchTouchEvent(ev);
}
}
`