gpt4 book ai didi

java - DialogFragment OnDismissListener 未被调用

转载 作者:行者123 更新时间:2023-12-01 19:43:36 25 4
gpt4 key购买 nike

我刚刚做了一个构建,注意到我的代码不起作用,在设置了一些断点之后,我看到,事实上,dismiss();方法被调用,但问题是监听器没有被调用,因此监听器内的代码永远不会被执行。我有我的 fragment A,在其中显示我的 DialogFragment (DateDialog),当 DateDialog 被解除时,我应该从我的 fragment A 中获取一些 DateDialog 值,但它不起作用。这是我原来的代码,它不起作用:

//DATEDIALOG
@Nullable
@Override

public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.datedialog, container, false);

localf = android.text.format.DateFormat.getMediumDateFormat(getContext());

TomorrowB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
setTomorrow();
HasDate = true;
dismiss();
}
});

return v;
}

//FRAGMENT A
DateB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final DateDialog d = new DateDialog();
d.show(getChildFragmentManagaer(), "DateDialog");
getChildFragmentManager().executePendingTransactions();
d.getDialog().setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
if (d.HasDate())
{
DoStuff();
}
}

});

然后,我决定更新它,改用一个界面,但它也不起作用。

    //DATEDIALOG
public interface DismissListener{
void onDialogDismiss(DateDialog d);
}

private DismissListener mListener;

public void setDismissListener(DismissListener listener){
mListener = listener;
}


@Nullable
@Override

public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.datedialog, container, false);

localf = android.text.format.DateFormat.getMediumDateFormat(getContext());

TomorrowB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
setTomorrow();
HasDate = true;
dismiss();
}
});

return v;
}


//FRAGMENT A

DateB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final DateDialog d = new DateDialog();
d.setDismissListener(new DateDialog.DismissListener() {
@Override
public void onDialogDismiss(DateDialog d) {
if (d.HasDate())
{
DoStuff();
}
}

我也尝试过 OnCancelListener() 但它是相同的。
PS:
这确实有效,直到前几天,我不知道我从支持库迁移到AndroidX库是否有什么关系。

最佳答案

根据 onCreateDialog documentation :

Note: DialogFragment own the Dialog.setOnCancelListener and Dialog.setOnDismissListener callbacks. You must not set them yourself. To find out about these events, override onCancel(DialogInterface) and onDismiss(DialogInterface).

情况一直如此。

您应该创建自己的回调接口(interface)并从 DateDialog 的 onDismiss() 调用该接口(interface),而不是直接调用 setOnDismissListener:

class DateDialog {
interface DismissListener {
void onDismiss(boolean hasDate)
}

private DismissListener listener = null;

public void setDismissListener(DismissListener listener) {
this.listener = listener;
}

@Override
public void onDismiss(DialogInterface dialog) {
super.onDismiss(dialog);
if (listener != null) {
listener.onDismiss(HasDate);
}
}
}

然后像这样使用它:

 final DateDialog d = new DateDialog();
d.show(getChildFragmentManager(), "DateDialog");
getChildFragmentManager().executePendingTransactions();
d.setDismissListener(new DismissListener() {
@Override
public void onDismiss(boolean hasDate) {
if (hasDate) {
DoStuff();
}
}
});

关于java - DialogFragment OnDismissListener 未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59148576/

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