ダイアログの外側を押すときにダイアログフラグを却下する方法は?
質問
私はaを使用しています 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
私のベース反応性として android.app.DialogFragment
私の断片として)。
機能するのは、次の2つの行のいずれかです。
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.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;
}
}