code

대화 상자 외부를 누를 때 DialogFragment를 닫는 방법은 무엇입니까?

codestyles 2020. 10. 20. 07:38
반응형

대화 상자 외부를 누를 때 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가 작동하지 않고 내 앱이 충돌했습니다.

(저는 AppCompatActivityBaseActivity 및 android.app.DialogFragmentFragment로 사용하고 있습니다).

작동하는 것은 다음 두 줄 중 하나입니다.

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;
            }
    }

참고 URL : https://stackoverflow.com/questions/8404140/how-to-dismiss-a-dialogfragment-when-pressing-outside-the-dialog

반응형