对于按下或触摸外部的后退按钮,AlertDialog 不调用 setOnCancelListener 和 setOnDismissListener

什么时候

  • 触摸对话框区域外部
  • 按后面的按钮

我期待 onDismiss(或 onCancel)将被调用。但是,它们都没有被调用。我能知道我遗漏了什么吗?从 AlertDialog setOnDismissListener 不工作,我认为 onCancel将被称为当我按下后退按钮。但对我没用。我可以知道我错过了什么吗?

public class RateAppDialogFragment extends SherlockDialogFragment {
public static RateAppDialogFragment newInstance() {
RateAppDialogFragment rateAppDialogFragment = new RateAppDialogFragment();
return rateAppDialogFragment;
}


@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.rate_app_dialog_fragment, null);


Utils.setCustomTypeFace(view, Utils.ROBOTO_LIGHT_TYPE_FACE);


final AlertDialog dialog = new AlertDialog.Builder(this.getSherlockActivity())
.setTitle("Love JStock?")
.setView(view)
// Add action buttons
.setPositiveButton("Rate 5 stars \u2605", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
Utils.showShortToast("Rate");
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Utils.showShortToast("No");
}
})
.setNeutralButton("Later", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Utils.showShortToast("Later");
}
})
.create();


dialog.setCanceledOnTouchOutside(true);


dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {


@Override
public void onCancel(DialogInterface dialog) {
Utils.showShortToast("Back button pressed?");
}
});


dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {


@Override
public void onDismiss(DialogInterface dialog) {
// TODO Auto-generated method stub
Utils.showShortToast("Back button pressed?");
}
});


return dialog;
}


}

    FragmentManager fm = fragmentActivity.getSupportFragmentManager();
if (fm.findFragmentByTag(RATE_APP_DIALOG_FRAGMENT) != null) {
return;
}


RateAppDialogFragment rateAppDialogFragment = RateAppDialogFragment.newInstance();
rateAppDialogFragment.show(fm, RATE_APP_DIALOG_FRAGMENT);
62235 次浏览

您还没有设置 backPressed键事件

dialog.setOnKeyListener(new Dialog.OnKeyListener() {
@Override
public boolean onKey(DialogInterface arg0, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_BACK) {
finish();
}
return true;
}
});

你应该读这个主题: 如何通过点击对话框外面的对话框来解除对话框?

Dialog dialog = new Dialog(context)
dialog.setCanceledOnTouchOutside(true);

当您使用 DialogFragment显示 Dialog时会出现问题

根据 http://developer.android.com/reference/android/app/DialogFragment.html,解决方案是在 DialogFragment中覆盖 onCancel

请注意从 http://developer.android.com/reference/android/app/DialogFragment.html#onCreateDialog(android.os.Bundle)

注意: DialogFragment 拥有 Dialog.setOnCancelListener 和 不能自己设置侦听器回调。 若要查找这些事件,请重写 onCancel (DialogInterface)和 OnDismiss (DialogInterface).

// This is DialogFragment, not Dialog
@Override
public void onCancel(DialogInterface dialog) {
}

在对话片段中,重写

@Override
public void onStop() {
super.onStop();
if (mListener != null) {
// do something here
}
}

如果使用 AlertDialog,请参见

builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
// dialog dismiss without button press
}
});

dialog.setCanceledOnTouchOutside(true)(谢谢@LeosLiterak)