Android: Can i show multiple Dialogs one over another? Is there something like Dialog Z-Level?

后端 未结 3 995
情话喂你
情话喂你 2021-01-12 09:50

Is it possible to show multiple Dialogs one over another? Is there something like Dialog Z-Level? I am using DialogFragment where user chooses elements, when he comfirms his

3条回答
  •  無奈伤痛
    2021-01-12 10:11

    Indeed, it's possible to show multiple dialog Fragments one inside another one. The z-order depends on the order they are created.

    In the code below there is an example of a FragmentActivity with the behavior that you require.

    public class MyActivity extends FragmentActivity {
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            //...    
        }
    
        public void onSave(View view) {
            Intent intent = getIntent();
    
            this.setResult(RESULT_OK, intent);
            finish();
        }
    
        public void onCancel(View view) {
            finish();
        }
    
        public void SelectWeekDay(View view) {
            DialogFragment selectWeekDayFragment = new SelectWeekDayFragment();
            selectWeekDayFragment.show(getSupportFragmentManager(), "WeekDayDialog");
        }
    
        public class SelectWeekDayFragment extends DialogFragment {
    
            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
                View view = inflater.inflate(R.layout.week_day_dialog, container, true);
    
                Button saveButton = (Button) view.findViewById(R.id.button_save);
                saveButton.setOnClickListener(new View.OnClickListener() {
    
                    @Override
                    public void onClick(View view) {
                        CheckBox checkboxMonday = (CheckBox) getDialog().findViewById(R.id.checkBox_monday);
                        if (!checkboxMonday.isChecked()) {
                            DialogFragment saveErrorFragment = new SaveErrorFragment();
                            saveErrorFragment.show(getSupportFragmentManager(), "SaveErrorFragment");
                        }
                        else {
                            SaveToDb(); //Perform actions to store on db or what you wish
                            dismiss();  
                        }
                    }
                });
    
                Button cancelButton = (Button) view.findViewById(R.id.button_cancel);
                cancelButton.setOnClickListener(new View.OnClickListener() {
    
                    @Override
                    public void onClick(View v) {
                        dismiss();
                    }
                });
    
                return view;    
            }
        }
    
        public class SaveErrorFragment extends DialogFragment {
    
            @Override
            public Dialog onCreateDialog(Bundle savedInstanceState) {
                return new AlertDialog.Builder(getActivity())
                .setMessage("You must select Monday").setPositiveButton("Ok", null).create();
            }
        }
    }
    

提交回复
热议问题