没有为后退按钮按下或触摸外部的AlertDialog调用setOnCancelListener和setOnDismissListener

Che*_*eng 60 android

什么时候

  • 触摸对话框区域外部
  • 按下后退按钮

我期待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;
    }

}
Run Code Online (Sandbox Code Playgroud)
    FragmentManager fm = fragmentActivity.getSupportFragmentManager();
    if (fm.findFragmentByTag(RATE_APP_DIALOG_FRAGMENT) != null) {
        return;
    }

    RateAppDialogFragment rateAppDialogFragment = RateAppDialogFragment.newInstance();
    rateAppDialogFragment.show(fm, RATE_APP_DIALOG_FRAGMENT);  
Run Code Online (Sandbox Code Playgroud)

Che*_*eng 136

当您使用DialogFragment显示时会发生此问题Dialog

http://developer.android.com/reference/android/app/DialogFragment.html,解决的办法是覆盖onCancelDialogFragment

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

注意:DialogFragment拥有Dialog.setOnCancelListener和Dialog.setOnDismissListener回调.你不能自己设置它们.要了解这些事件,请覆盖onCancel(DialogInterface)和onDismiss(DialogInterface).

// This is DialogFragment, not Dialog
@Override
public void onCancel(DialogInterface dialog) {
}
Run Code Online (Sandbox Code Playgroud)


Hug*_*sse 9

如果您正在使用AlertDialog,请参阅

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

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

  • java.lang.IllegalStateException:无法设置Dialog的OnCancelListener或OnDismissListener (2认同)

Sil*_*ler 5

您还没有设置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;
    }
});
Run Code Online (Sandbox Code Playgroud)