android dialog activity touch outside

BUG描述源地址:http://stackoverflow.com/questions/12102777/prevent-android-activity-dialog-from-closing-on-outside-touch

 

BUG现象:

  (1) UI界面弹出一个Dialog Activity;

  (2) 点击该Dialog Activity外的Button(该Button属于调用该Dialog Activity的Activity);

  (3) Button事件依然响应;

 

需求:

  当弹出Dialog后,该Dialog之外的所有组件不响应。

 

解决方式:

  在Dialog Activity中,添加如下代码: 

  setFinishOnTouchOutside(false);

 

 

[补充]:

  一、如果是普通Dialog,比如 AlertDialog,要实现点击该Dialog外部时,该Dialog不退出,请添加如下代码:

  (1) 在onCreate方法中:

     // Make us non-modal, so that others can receive touch events.

     getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);

     // ...but notify us that it happened.

     getWindow().setFlags(WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH);

  (2) 重写onTouch方法:

   @Override

     public boolean onTouchEvent(MotionEvent event) {

         if (MotionEvent.ACTION_OUTSIDE == event.getAction()) {

             return true;

         }

         return super.onTouchEvent(event);

     }

  

  二、如果是在DialogFragment中,要实现点击该Dialog外部时,该Dialog不退出,请添加如下代码:

  @Override

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

       ...

       getDialog().setCanceledOnTouchOutside(false);

       ... 

    }

      该问题解决源地址:http://stackoverflow.com/questions/8404140/how-to-dismiss-a-dialogfragment-when-pressing-outside-the-dialog

你可能感兴趣的:(Activity)