繁体   English   中英

如何通过在对话框外单击来关闭对话框?

[英]How to dismiss the dialog with click on outside of the dialog?

我已经为我的应用程序实现了一个自定义对话框。 我想实现当用户在对话框外单击时,对话框将被关闭。 我该怎么做?

您可以使用dialog.setCanceledOnTouchOutside(true); 如果您触摸对话框外部,它将关闭对话框。

就像是,

  Dialog dialog = new Dialog(context)
  dialog.setCanceledOnTouchOutside(true);

或者,如果您的 Dialog 在非模型中,则

1 - 为对话框的窗口属性设置FLAG_NOT_TOUCH_MODAL

Window window = this.getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);

2 - 向窗口属性添加另一个标志, FLAG_WATCH_OUTSIDE_TOUCH - 这个标志用于对话框在其可见区域之外接收触摸事件。

3 - 覆盖对话框的onTouchEvent()并检查操作类型。 如果操作类型为“ MotionEvent.ACTION_OUTSIDE ”,则表示用户在对话区域外进行交互。 因此,在这种情况下,您可以关闭对话或决定要执行的操作。 查看普通版?

public boolean onTouchEvent(MotionEvent event)  
{  

       if(event.getAction() == MotionEvent.ACTION_OUTSIDE){  
        System.out.println("TOuch outside the dialog ******************** ");  
               this.dismiss();  
       }  
       return false;  
}  

有关更多信息,请查看如何根据接触点关闭自定义对话框? 以及如何在对话框区域外触摸时关闭非模态对话框

只需使用

dialog.setCanceledOnTouchOutside(true);

您可以使用 onTouchEvent 的这个实现。 它防止对触摸事件的底层活动做出反应(如howettl 所述)。

@Override
public boolean onTouchEvent ( MotionEvent event ) {
  // I only care if the event is an UP action
  if ( event.getAction () == MotionEvent.ACTION_UP ) {
    // create a rect for storing the window rect
    Rect r = new Rect ( 0, 0, 0, 0 );
    // retrieve the windows rect
    this.getWindow ().getDecorView ().getHitRect ( r );
    // check if the event position is inside the window rect
    boolean intersects = r.contains ( (int) event.getX (), (int) event.getY () );
    // if the event is not inside then we can close the activity
    if ( !intersects ) {
      // close the activity
      this.finish ();
      // notify that we consumed this event
      return true;
    }
  }
  // let the system handle the event
  return super.onTouchEvent ( event );
}

资料来源: http : //blog.twimager.com/2010/08/closure-activity-by-touching-outside.html

或者,如果您使用样式 xml 中定义的主题自定义对话框,请将此行放在您的主题中:

<item name="android:windowCloseOnTouchOutside">true</item>
dialog.setCanceledOnTouchOutside(true); 

在外面触摸时关闭对话框。

如果您不想在外部触摸时关闭,请使用以下代码:

dialog.setCanceledOnTouchOutside(false);

此方法应完全避免灰色区域下方的活动检索点击事件。

如果有,请删除此行:

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

把这个放在你创建的活动上

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

然后用这个覆盖触摸事件

@Override
public boolean onTouchEvent(MotionEvent ev)
{
    if(MotionEvent.ACTION_DOWN == ev.getAction())
    {
        Rect dialogBounds = new Rect();
        getWindow().getDecorView().getHitRect(dialogBounds);
        if (!dialogBounds.contains((int) ev.getX(), (int) ev.getY())) {
            // You have clicked the grey area
            displayYourDialog();
            return false; // stop activity closing
        }
    }

    // Touch events inside are fine.
    return super.onTouchEvent(ev);
}

此代码用于当使用单击对话框时隐藏软输入以及当用户单击对话框外侧时软输入和对话框都关闭时。

dialog = new Dialog(act) {
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // Tap anywhere to close dialog.
        Rect dialogBounds = new Rect();
        getWindow().getDecorView().getHitRect(dialogBounds);
        if (!dialogBounds.contains((int) event.getX(),
                (int) event.getY())) {
            // You have clicked the grey area
            InputMethodManager inputMethodManager = (InputMethodManager) act
                    .getSystemService(act.INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(dialog
                    .getCurrentFocus().getWindowToken(), 0);
            dialog.dismiss();
            // stop activity closing
        } else {
            InputMethodManager inputMethodManager = (InputMethodManager) act
                    .getSystemService(act.INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(dialog
                    .getCurrentFocus().getWindowToken(), 0);
        }

        return true;
    }
};

你可以试试这个:-

AlterDialog alterdialog;
alertDialog.setCanceledOnTouchOutside(true);

或者

alertDialog.setCancelable(true);

如果你有一个AlterDialog.Builder那么你可以试试这个:-

alertDialogBuilder.setCancelable(true);

另一种解决方案,此代码取自Window android 源代码您应该将这两个方法添加到您的对话框源代码中。

@Override
public boolean onTouchEvent(MotionEvent event) {        
    if (isShowing() && (event.getAction() == MotionEvent.ACTION_DOWN
            && isOutOfBounds(getContext(), event) && getWindow().peekDecorView() != null)) {
        hide();
    }
    return false;
}

private boolean isOutOfBounds(Context context, MotionEvent event) {
    final int x = (int) event.getX();
    final int y = (int) event.getY();
    final int slop = ViewConfiguration.get(context).getScaledWindowTouchSlop();
    final View decorView = getWindow().getDecorView();
    return (x < -slop) || (y < -slop)
            || (x > (decorView.getWidth()+slop))
            || (y > (decorView.getHeight()+slop));
}

这个解决方案没有这个问题:

除了下面的活动也会对触摸事件做出反应之外,这很有效。 有什么方法可以防止这种情况吗? – 豪威特

调用dialog.setCancelable(false); 来自您的活动/片段。

以下对我有用:

myDialog.setCanceledOnTouchOutside(true);

我尝试了一些答案,但仍然遇到一个问题,例如当我在对话框外按下时隐藏但显示变暗的视图,再次按下将转到父活动。 但实际上我想在第一次点击后去父活动。 所以我所做的是

dialog.setOnCancelListener(this);

并更改了我的活动以实现DialogInterface.OnCancelListener

@Override
public void onCancel(DialogInterface dialog) {
     finish();
}

繁荣,它奏效了。

您可以将占据所有屏幕尺寸的background设为transparent然后监听onClick事件以dismiss它。

这是代码

    dialog.getWindow().getDecorView().setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent ev) {

            if(MotionEvent.ACTION_DOWN == ev.getAction())
            {
                Rect dialogBounds = new Rect();
                dialog. getWindow().getDecorView().getHitRect(dialogBounds);
                if (!dialogBounds.contains((int) ev.getX(), (int) ev.getY())) {
                    // You have clicked the grey area
                    UiUtils.hideKeyboard2(getActivity());
                    return false; // stop activity closing
                }
            }
            getActivity().dispatchTouchEvent(ev);
            return false;
        }
    });

试试这个。 当你触摸外面时,你可以隐藏键盘

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM