允许外部触摸查询DialogFragment [英] Allow outside touch for DialogFragment
问题描述
我有一个片段
在我的应用程序,显示了 DialogFragment
。
我在片段一个按钮,关闭对话框。但是,当我展示dialogFragment,外面倒是从对话框什么也不做,我无法从对话片段外单击按钮。
I have a Fragment
in my app that shows a DialogFragment
.
I have in the fragment a button that closes the dialog. But when I show the dialogFragment, the touches outside from the dialog do nothing and I can't click the buttons outside from the dialog fragment.
如何才能让外界接触的DialogFragment?
How can I allow outside touches for DialogFragment?
推荐答案
为了做到这一点,在窗口
,允许外界接触,应开启的标志而对于美观的背景昏暗的标志应该被清除。
由于在创建对话框后,一定要做,我已经通过了处理程序实现了它
。
In order to do that, a flag of the Window
that allows the outside touch should be turned on and for the good appearance the background dim flag should be cleared.
Since it must be done after dialog is created, I've implemented it via the Handler
.
@Override
public void onViewCreated(final View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// This is done in a post() since the dialog must be drawn before locating.
getView().post(new Runnable() {
@Override
public void run() {
Window dialogWindow = getDialog().getWindow();
// Make the dialog possible to be outside touch
dialogWindow.setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
dialogWindow.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
getView().invalidate();
}
});
}
目前时刻外部触摸是可能的。
At this moment the outside touch is possible.
在情况下,我们要使它更好,没有框架,下面code,可补充:
In case we want to make it nicer and without the frame, the following code can be added:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Hide title of the dialog
setStyle(STYLE_NO_FRAME, 0);
}
这篇关于允许外部触摸查询DialogFragment的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!