Android:我可以一次显示多个对话框吗?是否有类似Dialog Z-Level的东西? [英] Android: Can i show multiple Dialogs one over another? Is there something like Dialog Z-Level?

查看:769
本文介绍了Android:我可以一次显示多个对话框吗?是否有类似Dialog Z-Level的东西?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以显示多个对话框?是否有类似Dialog Z-Level的东西? 我正在使用DialogFragment,用户在其中选择元素,当他确认选择时,它将保存到数据库并在服务器上发送.如果保存操作失败,我想通知用户...是否可能出现另一个对话框?而且不会清除我的第一个对话框吗? 预先感谢.

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 choice, it is saved to database and sent on server. if the save action fails I would like to inform user with ... another dialog is it possible? And will it not clear off my first dialog? Thanks in advance.

推荐答案

实际上,可以在一个对话框中显示多个对话框片段. z顺序取决于它们的创建顺序.

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

下面的代码中有一个FragmentActivity的示例,具有您所需的行为.

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

这篇关于Android:我可以一次显示多个对话框吗?是否有类似Dialog Z-Level的东西?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆