簡體   English   中英

在對話框外按下時如何關閉 DialogFragment?

[英]How to dismiss a DialogFragment when pressing outside the dialog?

我正在使用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作為我的片段)。

有效的是以下兩行之一:

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外部單擊時執行某些邏輯,只需覆蓋DialogFragment方法。

override fun onCancel(dialog: DialogInterface) {
    super.onCancel(dialog)
    // Do your work here
}
DialogFragment.getDialog().setCanceledOnTouchOutside(false);

是打錯字了。 我有同樣的問題。 這適用於 Java 和 Mono for android Mono 將是:

this.getDialog().SetCanceledOnTouchOutside(false);
            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;
            }
    }

我建議僅在嘗試上述解決方案后才使用我的解決方案。 在這里描述了我的解決方案。 簡單地說,我正在檢查 DialogFragment.getView() 的觸摸邊界。 當接觸點在 DialogFragment 之外時,我將關閉對話框。

public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup
container, @Nullable Bundle savedInstanceState) 
{
        DialogConfermationdialogBinding binding = DialogConfermationdialogBinding.inflate(inflater,container,false);
        View view = binding.getRoot();

        getDialog().setCanceledOnTouchOutside(false);

        return view;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM