gpt4 book ai didi

android - 关闭 DialogFragment(不是 Dialog)onTouchOutside

转载 作者:IT王子 更新时间:2023-10-28 23:28:16 26 4
gpt4 key购买 nike

我已经搜索了有关关闭对话框 onTouchOutside 的所有答案,但是,我在我的应用程序中使用了 DialogFragment。当用户在 DialogFragment 的 区域之外点击时,如何实现关闭 DialogFragment

我检查了 Dialogsource code对于 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 的方法,并在可能的情况下使用示例代码?

最佳答案

答案很简单:

MyDialogFragment fragment = new MyDialogFragment(); // init in onCreate() or somewhere else
...
if ( fragment.getDialog() != null )
fragment.getDialog().setCanceledOnTouchOutside(true); // after fragment has already dialog, i. e. in onCreateView()

http://developer.android.com/reference/android/app/DialogFragment.html#setShowsDialog%28boolean%29有关 DialogFragments 中的对话框的更多信息。

关于android - 关闭 DialogFragment(不是 Dialog)onTouchOutside,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8734303/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com