按下后退按钮时自定义 AlertDialog 消失

Custom AlertDialog disappears on back button press

大家好,我创建了一个自定义警报对话框。在生成器中,我将 cancelable 设置为 false,但当我按下后退按钮时它仍然消失,有什么想法吗?

这是对话框的代码:

public final class HemisphereDialogFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Use the Builder class for convenient dialog construction
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    LayoutInflater inflater = getActivity().getLayoutInflater();
    View customTitle = inflater.inflate(R.layout.hemisphere_dialog_custom_title, null);
    builder.setCustomTitle(customTitle);
    String[] entries = new String[2];
    entries[0] = getResources().getString(R.string.northern_hemisphere);
    entries[1] = getResources().getString(R.string.southern_hemisphere);
    builder.setItems(entries, new DialogInterface.OnClickListener() {
        //The 'which' argument contains the index position of the selected item
        public void onClick(DialogInterface dialog, int which) {
            if( which == 0 ) {
                GlobalVariables.getShared().setIsInNorthernHemisphere(true);
            } else if( which == 1 ) {
                GlobalVariables.getShared().setIsInNorthernHemisphere(false);
            }

            ToolbarActivity.outfitsFragment.hemisphereSelected();
            GlobalVariables.getShared().setHasAskedForHemisphere(true);
        }
    });
    builder.setCancelable(false);

    //Create the AlertDialog object and return it
    return builder.create();
}

这是它的显示方式:

new HemisphereDialogFragment().show(getSupportFragmentManager(), "hemisphereDialog");

另一个小问题,有没有办法更改对话框中项目的文本大小?

您将警报对话框的可取消设置为 false,但片段仍设置为可取消,您还需要将 setCancelable(false) 添加到片段中。

您应该在对话框片段本身而不是 AlertDialog.Builder 中调用 setCancellable(false)。