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

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

微信公众号:终端研发部
(欢迎关注学习和交流) 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

androidstarjack

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值