gpt4 book ai didi

android - 如何防止 DialogFragment 在外部触摸时消失?

转载 作者:行者123 更新时间:2023-12-05 00:12:24 26 4
gpt4 key购买 nike

我正在使用 DialogFragment,它将显示完全自定义的 View ,不使用 native 按钮。我使用 dialog.SetCanceledOnTouchOutside(true); 将对话框设置为可取消;当用户触摸外部时,我想运行一些代码来确定是否应该关闭对话框。我的问题是,当一个事件被触发(取消、解除)时,对话框甚至在 super.Dismiss(); 之前就已经解除了。所以我永远无法绕过解雇。

我尝试了很多东西,重写 fragment 的 OnDismiss 和 OnCancel(触发但迟到),在 dialogBu​​ilder 和/或对话框本身上设置监听器(未触发或为时已晚),重写 OnShow 以重置听众等...

遇到这个问题,有什么想法吗?

附言:使用 Xamarin

public override Dialog OnCreateDialog(Bundle savedInstanceState)
{
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this.Activity);
alertDialogBuilder.SetView(myView);
AlertDialog dialog = alertDialogBuilder.Create();
dialog.SetCanceledOnTouchOutside(true);
return dialog;
}

public override void OnDismiss(IDialogInterface dialog)
{
//BreakPoint here, already dismissed
base.OnDismiss(dialog);
}

最佳答案

你想达到这个结果吗?如果点击Dialog外部,我们可以检测到Dialog关闭前的点击事件。 enter image description here

您可以创建一个 MyAlertDialog 继承 AlertDialog

    public class MyAlertDialog:AlertDialog
{
public MyAlertDialog(Context c):base(c) {

}

protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
//Please add following code for watching outside touch
this.Window.SetFlags(WindowManagerFlags.NotTouchModal, WindowManagerFlags.NotTouchModal);
this.Window.AddFlags(WindowManagerFlags.WatchOutsideTouch);
}



public override bool OnTouchEvent(MotionEvent e)
{
//just used for testing.
var sss = "test";
//If you want to close the Dialog when click the outside the Dialog part.
//this.Dismiss();
return true;
}

}

接下来的部分是DialogFragment

    public class MyDialog:Android.Support.V4.App.DialogFragment
{

public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);

}


public override Dialog OnCreateDialog(Bundle savedInstanceState)
{


MyAlertDialog alertDialogBuilder = new MyAlertDialog(this.Activity);
alertDialogBuilder.SetTitle("MyDialog");
alertDialogBuilder.SetCanceledOnTouchOutside(true);
return alertDialogBuilder;
}



public override void OnPause()
{
base.OnPause();
}

public override void OnCancel(IDialogInterface dialog)
{
base.OnCancel(dialog);
}

public override void OnDismiss(IDialogInterface dialog)
{
base.OnDismiss(dialog);
}
}

点击按钮推送对话框。

private void BtnClick_Click(object sender, System.EventArgs e)
{
MyDialog dialog = new MyDialog();
dialog.Show(SupportFragmentManager, "dialog");
}

关于android - 如何防止 DialogFragment 在外部触摸时消失?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59407161/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com