调用setOnCancelListener和setOnDismi

调用setOnCancelListener和setOnDismi

本文介绍了按下后退按钮或在外部触摸时,不会为AlertDialog调用setOnCancelListener和setOnDismissListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

何时

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

我希望将调用onDismiss(或onCancel).但是,它们都不被调用.我可以知道我缺少什么吗?从 AlertDialog setOnDismissListener无法正常工作,我以为当我按下后退按钮时会调用onCancel.但这对我不起作用.我可以知道我错过了什么吗?

I'm expecting onDismiss (Or onCancel) will be called. However, both of them are not called. May I know is there anything I'm missing? From AlertDialog setOnDismissListener not working, I thought onCancel will be called when I press back button. But it doesn't work for me. May I know is there anything I had missed out?

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);

推荐答案

使用DialogFragment显示Dialog

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

According to http://developer.android.com/reference/android/app/DialogFragment.html, the solution is to override onCancel in DialogFragment

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

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

这篇关于按下后退按钮或在外部触摸时,不会为AlertDialog调用setOnCancelListener和setOnDismissListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 18:58