您的位置:首页 > 其它

dialog形式的activity屏幕触摸事件总结

2017-01-04 21:56 330 查看
Dialog大家都在项目中会用到,前两天在项目中我们要弹出一个Activity形式的dialog,大家都知道dialog本身提供了触摸屏幕事件:

比如说dialog的setCanceledOnTouchOutside方法,而弹出一个activity形式的dialog如何去设置呢?

首先我们看下dialog:

/**
* Sets whether this dialog is canceled when touched outside the window's
* bounds OR pressed the back key. If setting to true, the dialog is
* set to be cancelable if not
* already set.
*
* @param cancel Whether the dialog should be canceled when touched outside
* the window OR pressed the back key.
*/
public MaterialDialog setCanceledOnTouchOutside(boolean cancel) {
this.mCancel = cancel;
if (mBuilder != null) {
mBuilder.setCanceledOnTouchOutside(mCancel);
}
return this;
}
大概意思就是:大概意思就是当初莫屏幕外会点击返回键的时候dialog是否可以关闭,如果设置true,可以取消,点击返回键可以关闭 掉。对于部分4.0手机可能不起作用。

大家要注意。关于dialog大家在熟悉不过,这里不做 过多的说明。

有关于弹出来一个Theme形式的activity,如何实现向dialog一样点击屏幕不消失呢?

这里需要在activity的方法里写入两个标识:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//getWindow().setFlags(WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
initVoidData();
init();
}
其中:

/** Window flag: even when this window is focusable (its
* {@link #FLAG_NOT_FOCUSABLE} is not set), allow any pointer events
* outside of the window to be sent to the windows behind it.  Otherwise
* it will consume all pointer events itself, regardless of whether they
* are inside of the window. */
public static final int FLAG_NOT_TOUCH_MODAL    = 0x00000020;
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL大体意思是用于窗口标志位的表示,将消耗触摸事件,进行行向下传递。

而第二个则是:
/** Window flag: if you have set {@link #FLAG_NOT_TOUCH_MODAL}, you
* can set this flag to receive a single special MotionEvent with
* the action
* {@link MotionEvent#ACTION_OUTSIDE MotionEvent.ACTION_OUTSIDE} for
* touches that occur outside of your window.  Note that you will not
* receive the full down/move/up gesture, only the location of the
* first down as an ACTION_OUTSIDE.
*/
public static final int FLAG_WATCH_OUTSIDE_TOUCH = 0x00040000;
大体意思是如果你设置了这个标识,则你可以通过这个标识接收一个特殊的动作,触摸发生在你的窗口之外。 注意,你不会接收完整的向下/移动/向上手势,只有位置第一次作为ACTION_OUTSIDE。此时触摸事件向下传递。

如果你觉得此文对您有所帮助,欢迎入群 QQ交流群 :232203809

微信公众号:终端研发部

(欢迎关注学习和交流) 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: