我已经搜索了有关关闭 Dialog onTouchOutside的所有答案,但是,我在我的应用程序中使用了 DialogFragment。当用户在DialogFragment区域之外单击时,如何实现关闭DialogFragment。
我检查了Dialog的setCanceledOnTouchOutside的源代码
public void setCanceledOnTouchOutside(boolean cancel) {
if (cancel && !mCancelable) {
mCancelable = true;
}
mCanceledOnTouchOutside = cancel;
}
还有另一个可能很有趣的函数isOutOfBounds
private boolean isOutOfBounds(MotionEvent event) {
final int x = (int) event.getX();
final int y = (int) event.getY();
final int slop = ViewConfiguration.get(mContext).getScaledWindowTouchSlop();
final View decorView = getWindow().getDecorView();
return (x < -slop) || (y < -slop)
|| (x > (decorView.getWidth()+slop))
|| (y > (decorView.getHeight()+slop));
}
但我想不出一种将这些用于DialogFragment的方法
除了这些,我还使用hierarchyviewer检查了应用程序的状态, 据我所知,我只能看到对话框的区域,而不是它的外部部分(我的意思是DialogFragment之后的屏幕剩余部分)。
您能否建议一种为DialogFragment实现此setCanceledOnTouchOutside的方法,并在可能的情况下使用示例代码?