首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >作为片段进行管理时,如何将进度对话框设置为不可取消?

作为片段进行管理时,如何将进度对话框设置为不可取消?
EN

Stack Overflow用户
提问于 2011-09-08 01:44:49
回答 2查看 18.6K关注 0票数 18

我使用的是作为Fragment托管的ProgressDialog。即使我将ProgressDialog设置为不可取消,BACK按钮仍将操作以将该Fragment从堆栈中删除。我的内部类看起来像这样:

代码语言:javascript
复制
public static class ProgressDialogFragment extends DialogFragment {

    private DialogStyle dialogStyle;

    public static ProgressDialogFragment newInstance(String title, String message) {
        ProgressDialogFragment fragment = new ProgressDialogFragment();
        Bundle args = new Bundle();
        args.putString("title", title);
        args.putString("message", message);
        fragment.setArguments(args);

        return fragment;
    }

    public void setDialogStyle(DialogStyle dialogStyle) {
        this.dialogStyle = dialogStyle;
    }


    @Override
    public ProgressDialog onCreateDialog(Bundle savedInstanceState) {
        String title = getArguments().getString("title");
        String message = getArguments().getString("message");

        ProgressDialog progressDialog = new ProgressDialog(getActivity());
        progressDialog.setTitle(title);
        progressDialog.setMessage(message);

        if(dialogStyle!=null) {
            switch (dialogStyle) {
                case CANCELABLE:
                    progressDialog.setCancelable(true);
                    break;
                case NON_CANCELABLE:
                    progressDialog.setCancelable(false);
                    break;

            }
        } else {
            progressDialog.setCancelable(false);
        }

        progressDialog.show();

        return progressDialog;
    }
}

然后我公开的方法是:

代码语言:javascript
复制
public void showProgressDialog(String title, String message, DialogStyle dialogStyle) {
            Fragment prev = fragmentManager.findFragmentByTag("progress dialog");
            if(prev!=null) {
                ft.remove(prev);
            }
            ft.addToBackStack(null);

            DialogFragment newFragment = ProgressDialogFragment.newInstance(title, message);
            ((ProgressDialogFragment)newFragment).setDialogStyle(dialogStyle);
            newFragment.show(fragmentManager, "progress dialog");
        }

因此,这里明显的混淆是,BACK按钮删除了ProgressDialog,因为它是作为Fragment进行管理的。那么,我如何才能使Dialog不可取消呢?

尝试像这样的东西似乎很奇怪:

代码语言:javascript
复制
@Override
    public void onBackPressed() {
        if(fragmentManager.fragmentManager.findFragmentByTag("progress dialog")!=null) {

        }
    }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-09-08 02:15:20

为什么不在DialogFragment上试用setCancelable(false)而不是ProgressDialog呢?

票数 39
EN

Stack Overflow用户

发布于 2014-02-21 17:08:46

您还可以在ProgressDialog上使用setCancelable(false)

票数 11
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7338199

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档