【问题标题】:How can I open a ProgressDialog on top of a DialogFragment?如何在 DialogFragment 上打开 ProgressDialog?
【发布时间】:2014-03-14 13:22:17
【问题描述】:

我有这个带有按钮的 Android 活动/布局。当我单击此按钮时,它会打开一个带有 2 个微调器的 DialogFragment。当 DialogFragment 出现时,我需要使用 Web 服务返回的项目填充这 2 个微调器。因此,在等待服务返回时,我想在刚刚打开的 DialogFragment 上显示一个 ProgressDialog。

问题是我似乎无法在 DialogFragment 之上创建 ProgressDialog。我可以看到 ProgressDialog 显示在 DialogFragment 后面,是否可以让 ProgressDialog 显示在其他所有内容之上?

我的代码是这样的:

public class RegionalFragment extends DialogFragment {

    private View view;
    private Activity activity;
    private Spinner spinner1;
    private Spinner spinner2;
    private ProgressDialog progressDialog;

    public RegionalFragment(Activity activity) {
        this.activity = activity;
        LayoutInflater inflater = activity.getLayoutInflater();
        view = inflater.inflate(R.layout.spinner_selection, null);
        this.progressDialog = new ProgressDialog(activity);
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder =
            new AlertDialog.Builder(activity)
                    .setTitle(R.string.title)
                    .setView(view)
                    .setPositiveButton(R.string.ok, new MyListener(activity))
                    .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            getDialog().cancel();
                        }
                    });

        progressDialog.getWindow().setBackgroundDrawable(new ColorDrawable(1));
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setCancelable(false);
        progressDialog.setIndeterminate(true);
        progressDialog.setMessage("Please wait...");
        progressDialog.show();

        invokeAsyncWebService();

    return builder.create();
}

谢谢!

【问题讨论】:

    标签: android progressdialog android-dialogfragment


    【解决方案1】:

    显然这是一个广泛抱怨ProgressDialog 的主题,我建议您尝试使用构建器创建的普通对话框,其布局(由您自定义)包括进度条或加载或您需要的任何内容。这应该可以解决您的问题。

    【讨论】:

      【解决方案2】:

      当我的父 DialogFragment 由于配置更改而重新创建时,我也遇到了问题。我无法让 progressDialog 再次显示在顶部。 解决方案是在 onResume() 事件中显示 progressDialog:

      @Override
      public void onResume() {
          super.onResume();
          if (inProgress) {
              progressDialog = ProgressDialog.show(ctx, "Report request", "connecting...", true);
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-11-24
        • 1970-01-01
        • 1970-01-01
        • 2023-03-27
        • 2016-12-21
        • 1970-01-01
        • 1970-01-01
        • 2019-01-19
        相关资源
        最近更新 更多