进度完成后如何更改进度对话框布局?

How to change a progress dialog layout when the progress completed?

我的 AsyncTask 扩展 class 中有一个进度对话框。 我已经使用 setcontentview() 将布局设​​置为进度对话框。 我想更改进度对话框的设计并在完成时显示 2 秒(即在 PostExecute() 中)。

我在 onPreexecute() 中启动进度对话框并在 onPostexecute() 方法中关闭它。

这就是我在 onPreexecute 中初始化进度对话框的方式

progressDialog = ProgressDialog.show(context, null, null);
progressDialog.setContentView(R.layout.progress_meditate);
progressDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
progressDialog.setProgressDrawable(new ColorDrawable(Color.WHITE));

我在 onPostExecute 中尝试了以下代码

progressDialog.setContentView(R.layout.progress_complete);
sleep(2000);
progressDialog.dismiss();

但它不起作用。我的 progress_complete 布局未显示。使用相同的设计,进度对话框在休眠时间后消失。

01。第一步初始化 progressBar 和 ProgressBarText

 private ProgressBar progressBar;
 private TextView progressBarText;

02。第二步声明这个OnCreate方法

  progressBar.setVisibility(View.VISIBLE);
  progressBarText.setVisibility(View.VISIBLE);

03。第三步声明这个onPostExecute方法

  progressBar.setVisibility(View.GONE);
  progressBarText.setVisibility(View.GONE);

就是这样。我认为这对你有帮助。

为此,您无法在运行时更改设计。在 post execute 上显示具有不同设计的对话框的唯一可能方法是 创建一个新对话框并关闭前一个对话框并显示新对话框。

不太简单的解决方案是扩展或创建您自己的 ProgressDialog。 另一方面,在 Android 文档中,您可以找到 following:

Caution: Android includes another dialog class called ProgressDialog that shows a dialog with a progress bar. This widget is deprecated because it prevents users from interacting with the app while progress is being displayed. If you need to indicate loading or indeterminate progress, you should follow the design guidelines for Progress & Activity and use a ProgressBar in your layout, instead of using ProgressDialog.

我会 suggest/recommend 转移到 ProgressBar 作为解决方案的起点,但这是你的决定。

我认为解决您的问题会更容易,因为 ProgressBar 只是另一个可以在完成时重叠的视图。

然后创建两个进度对话框,一个在另一个之上,先关闭一个,2 秒后关闭另一个。它会起作用