onTouchEvent 何时调用 ACTION_OUTSIDE

参见Dialog的点击Dialog外关闭Dialog

要想获取该事件, 必须要求当前Window的包含FLAG_WATCH_OUTSIDE_TOUCH

public class DialogOutSide extends Dialog{
    public DialogOutSide(Context context) {
        super(context);
 init();
 }

    protected DialogOutSide(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
 init();
 } 

    public DialogOutSide(Context context, int themeResId) {
        super(context, themeResId);
 init();
 }

    @Override
 public boolean onTouchEvent(MotionEvent event) {
 if (MotionEvent.ACTION_OUTSIDE == event.getAction()) {
            Log.i("yyy","outside");
 return true;
 }


 return super.onTouchEvent(event);
 }


    private void init(){
        setContentView(R.layout.dialog_outside);
 //清空
 getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
 //设置 这种模式当触发outside touch时响应
 getWindow().setFlags(WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH);
 }
}

你可能感兴趣的:(Android)