대화 상자 외부를 누를 때 DialogFragment를 닫는 방법은 무엇입니까?
를 사용 DialogFragment
하고 있으며 눌렀을 때 대화 상자를 닫도록 (즉, 닫기) 이미지를 성공적으로 설정했지만 사용자가 대화 상자 외부를 클릭 할 때 대화 상자를 닫는 방법을 찾기가 어렵습니다. 일반 대화 상자. 나는 일종의
dialogFragment.setCanceledOnTouchOutside(true);
전화했지만 문서에는 보이지 않습니다.
이것이 가능 DialogFragment
합니까? 아니면 잘못된 곳을 찾고 있습니까? 나는 '부모'활동에서 터치 이벤트를 가로 채려고했지만 터치 이벤트가없는 것 외에는 나에게 옳지 않은 것 같았다.
DialogFragment.getDialog().setCanceledOnTouchOutside(true);
onCreateView
(Apurv Gupta가 지적한대로) 반드시 호출되어야합니다 .
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
...
getDialog().setCanceledOnTouchOutside(true);
...
}
/** The system calls this only when creating the layout in a dialog. */
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// The only reason you might override this method when using onCreateView() is
// to modify any dialog characteristics. For example, the dialog includes a
// title by default, but your custom layout might not need it. So here you can
// remove the dialog title, but you must call the superclass to get the Dialog.
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCanceledOnTouchOutside(true);
return dialog;
}
여기에 많은 답변이 있지만 대화 상자가 열리면 앱이 충돌합니다. getDialog().setCanceledOnTouchOutside(true);
내부 쓰기 onCreateView
가 작동하지 않고 내 앱이 충돌했습니다.
(저는 AppCompatActivity
BaseActivity 및 android.app.DialogFragment
Fragment로 사용하고 있습니다).
작동하는 것은 다음 두 줄 중 하나입니다.
getDialog (). setCanceledOnTouchOutside (true);
또는
this.getDialog (). setCanceledOnTouchOutside (true);
onActivityCreated
같은 내부
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//getDialog().getWindow().getAttributes().windowAnimations = R.style.DialogAnimationZoom;
//getDialog().getWindow().setDimAmount(0.85f);
getDialog().setCanceledOnTouchOutside(true);//See here is the code
}
사용하지 말아야 할 것 :
DialogFragment.getDialog (). setCanceledOnTouchOutside (false);
다음 오류가 발생합니다.
그리고 코드를 작성하면 onCreateView
앱이 충돌합니다! 잘못된 것을 찾으면 답변을 업데이트하십시오.
DialogFragment.getDialog().setCanceledOnTouchOutside(false);
오해였다. 나는 같은 문제가 있었다. 이것은 Java에서 잘 작동하고 Android Mono의 Mono는 다음과 같습니다.
this.getDialog().SetCanceledOnTouchOutside(false);
위의 솔루션을 시도한 후에 만 솔루션을 사용하는 것이 좋습니다. 여기에 내 솔루션을 설명 했습니다 . 간단히 말해, DialogFragment.getView ()의 터치 경계를 확인하고 있습니다. 터치 포인트가 DialogFragment 외부에 있으면 Dialog를 닫습니다.
Dialog.SetCanceledOnTouchOutside(true);
나를 위해 일했다
내 코드
class dlgRegister : DialogFragment
{
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
....
....
}
public override void OnActivityCreated(Bundle savedInstanceState)
{
Dialog.Window.RequestFeature(WindowFeatures.NoTitle);
Dialog.SetCanceledOnTouchOutside(true);
base.OnActivityCreated(savedInstanceState);
Dialog.Window.Attributes.WindowAnimations = Resource.Style.dialog_animation;
}
}
'code' 카테고리의 다른 글
Android 스튜디오에서 buildToolsVersion '23 .0.1 '을 업데이트 한 후 메모리 부족 문제 (0) | 2020.10.20 |
---|---|
뷰 가시성이 View.GONE 인 경우 RelativeLayout 문제 (0) | 2020.10.20 |
Android int를 String으로 변환하는 방법은 무엇입니까? (0) | 2020.10.20 |
JavaScript에서 ISO 날짜를 날짜 형식 yyyy-mm-dd로 변환 (0) | 2020.10.20 |
Git은 파일 재설정 / 삭제를 거부합니다. (0) | 2020.10.19 |