簡體   English   中英

自定義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");

另一個小問題是,有沒有辦法更改對話框中各項的文本大小?

您將警報對話框的setCancelable(false)設置為false,但是您的片段仍設置為cancelable,您還需要將setCancelable(false)添加到您的片段中。

您應該在Dialog片段本身而不是AlertDialog.Builder中調用setCancellable(false)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM