不推荐使用ProgressDialog.使用的替代方法是什么?

Sun*_*l P 178 android progressdialog android-progressbar

我发现ProgressDialog现在已经弃用了.除了之外,还有什么可以代替它ProgressBar.我正在使用android studio版本2.3.3.

ProgressDialog progressDialog=new ProgressDialog(this);
progressDialog.show();
Run Code Online (Sandbox Code Playgroud)

Gow*_*n M 176

是的,在API level 26它已被弃用,你可以使用progressBar.

使用此代码以编程方式创建:

RelativeLayout layout = findViewById(R.id.display); //在此指定根布局ID

(要么)

RelativeLayout layout = findViewById(this);

RelativeLayout layout = findViewById(R.id.display);  //specify here Root layout Id
Run Code Online (Sandbox Code Playgroud)

要禁用用户交互,只需添加以下代码即可

RelativeLayout layout = findViewById(this);
Run Code Online (Sandbox Code Playgroud)

要恢复用户交互,您只需添加以下代码即可

android.R.attr.progressBarStyleSmall

仅供将来参考,android.R.attr.progressBarStyleHorizontal改为API level 21.

此下面的行代码仅适用于API 21 progressBar.setProgressTintList(ColorStateList.valueOf(Color.RED));


解决方案2:另一种方式

progressBar = new ProgressBar(youractivity.this, null, android.R.attr.progressBarStyleLarge);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(100, 100);
params.addRule(RelativeLayout.CENTER_IN_PARENT);
layout.addView(progressBar, params);
Run Code Online (Sandbox Code Playgroud)

Activity.java

progressBar.setVisibility(View.VISIBLE);
Run Code Online (Sandbox Code Playgroud)

更多细节:

1.参考一个

2.参考二

我希望这可以帮到你.

  • 那么,现在我需要一行代码来获得大约相同的结果?谷歌干得好! (14认同)
  • @David Thing 是,阻止 UI 正是我想要做的,因为我不希望用户在后台任务完成之前能够进行交互。 (7认同)
  • @David所以你是说谷歌提倡用户应该能够在后台线程准备应用程序数据时打开空下拉菜单,查看占位符项目并按下按钮?这真是可怕的设计!应显示“请稍候”对话框,最好以动画形式显示应用程序尚未停止,然后在应用程序准备好后允许用户与 UI 进行交互。但是,如果您的意思是“不要在 UI 线程上做繁重的事情”,那么这是有道理的。当我说“阻止 UI”时,我的意思是阻止用户交互,而不是在 UI 线程上进行工作。 (6认同)
  • 就像你一直在使用这个,然后你就像是的,进度对话框终于被弃用了,所以我可以向世界展示我的潜力:D,开玩笑,非常感谢你的详细回答。 (4认同)
  • 但是如何 setMessage() 呢? (3认同)
  • @David 那糟糕的应用程序行为如何?在应用程序准备就绪之前,用户不应该能够执行任何操作;否则你会遇到问题。几十年来,显示“请稍候”类型的对话框一直是一种相当标准的方式。 (2认同)

Ous*_*oua 27

此类在API级别26中已弃用.ProgressDialog是一个模式对话框,可防止用户与应用程序进行交互.您应该使用ProgressBar等进度指示器,而不是使用此类,可以将其嵌入到应用程序的UI中.或者,您可以使用通知来通知用户任务的进度.链接

Android O由于Google新的UI标准,它已被弃用

  • "由于谷歌的新UI标准,它在Android O上已被弃用" - 链接请新的UI标准 (24认同)
  • 否决它的原因是因为它阻止了用户输入?有时候,我想知道Google工程师正在打哪种粉末。使用阻止对话框比必须隐藏/禁用输入控件然后启用进度条要容易得多。Google的愚蠢思想。 (11认同)
  • 我猜他们更感兴趣的是防止滥用模式对话框:有时,它们会让用户等待操作结束,而用户根本不需要等待,但可以继续做其他事情,因此让他们不必要地等待会降低用户体验。尽管如此,在很多情况下,用户别无选择,只能等待操作完成,因此我同意对于这些情况应该有一个未弃用的标准方法,而不是强迫每个开发人员实现自己的方法。 (5认同)
  • @David 看看我的评论的赞成票。也告诉那些人。我认为谷歌的决定更具政治性。而且我仍在使用对话框。所以,开你和谷歌的玩笑。 (5认同)
  • Android 团队的狂妄自大单方面决定他们比我更了解我的客户的需求,这简直令人难以置信。阻止与 UI 的交互是一种非常常见的需求,并且过去可以通过“ProgressDialog”完美满足。现在我不得不浪费生命的另一部分时间来重新发明轮子。 (3认同)

Kis*_*nga 26

您可以使用以下代码AlertDialog作为ProgressDialog参考ProgressDialog.每次显示进度对话框时都需要调用此函数.

码:

    public void setProgressDialog() {

    int llPadding = 30;
    LinearLayout ll = new LinearLayout(this);
    ll.setOrientation(LinearLayout.HORIZONTAL);
    ll.setPadding(llPadding, llPadding, llPadding, llPadding);
    ll.setGravity(Gravity.CENTER);
    LinearLayout.LayoutParams llParam = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT);
    llParam.gravity = Gravity.CENTER;
    ll.setLayoutParams(llParam);

    ProgressBar progressBar = new ProgressBar(this);
    progressBar.setIndeterminate(true);
    progressBar.setPadding(0, 0, llPadding, 0);
    progressBar.setLayoutParams(llParam);

    llParam = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT);
    llParam.gravity = Gravity.CENTER;
    TextView tvText = new TextView(this);
    tvText.setText("Loading ...");
    tvText.setTextColor(Color.parseColor("#000000"));
    tvText.setTextSize(20);
    tvText.setLayoutParams(llParam);

    ll.addView(progressBar);
    ll.addView(tvText);

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setCancelable(true);
    builder.setView(ll);

    AlertDialog dialog = builder.create();
    dialog.show();
    Window window = dialog.getWindow();
    if (window != null) {
        WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
        layoutParams.copyFrom(dialog.getWindow().getAttributes());
        layoutParams.width = LinearLayout.LayoutParams.WRAP_CONTENT;
        layoutParams.height = LinearLayout.LayoutParams.WRAP_CONTENT;
        dialog.getWindow().setAttributes(layoutParams);
    }
}
Run Code Online (Sandbox Code Playgroud)

输出:

在此输入图像描述


Dev*_*e01 19

您可以简单地为进度条设计一个xml界面,并将其作为视图传递给AlertDialog,然后随时显示或关闭对话框.

progress.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:padding="13dp"
    android:layout_centerHorizontal="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ProgressBar
        android:id="@+id/loader"
        android:layout_marginEnd="5dp"
        android:layout_width="45dp"
        android:layout_height="45dp" />
    <TextView
        android:layout_width="wrap_content"
        android:text="Loading..."
        android:textAppearance="?android:textAppearanceSmall"
        android:layout_gravity="center_vertical"
        android:id="@+id/loading_msg"
        android:layout_toEndOf="@+id/loader"
        android:layout_height="wrap_content" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

显示进度对话框的代码.只需复制此代码并将其粘贴到您的片段即可.

   private void setDialog(boolean show){
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        //View view = getLayoutInflater().inflate(R.layout.progress);
        builder.setView(R.layout.progress);
        Dialog dialog = builder.create();
        if (show)dialog.show();
        else dialog.dismiss();
    }
Run Code Online (Sandbox Code Playgroud)

然后只要想要显示progressdialog就调用该方法,并将true作为参数传递给它来显示它,或者将false传递给false以关闭对话框.

  • @Alok Rajasukumaran 看来您正在使用活动,只需将其替换为 `this`,使其看起来像这样 `AlertDialog.Builder builder = new AlertDialog.Builder(this);`。请确保您复制的是正确的代码。 (2认同)
  • `layout_toEndOf` 可以被删除,因为不适用于 `LinearLayout` (2认同)

Sye*_*eem 15

ProgressBar非常简单易用,我打算将其作为简单的进度对话框.第一步是你可以制作你想要显示的对话框的xml布局,让我们说出这个布局的名称

layout_loading_dialog.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="20dp">
    <ProgressBar
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="4"
        android:gravity="center"
        android:text="Please wait! This may take a moment." />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

下一步是创建AlertDialog,它将使用ProgressBar显示此布局

AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setCancelable(false); // if you want user to wait for some process to finish,
builder.setView(R.layout.layout_loading_dialog);
AlertDialog dialog = builder.create();
Run Code Online (Sandbox Code Playgroud)

现在剩下的就是在我们这样的点击事件中显示和隐藏这个对话框

dialog.show(); // to show this dialog
dialog.dismiss(); // to hide this dialog
Run Code Online (Sandbox Code Playgroud)

这就是它,它应该工作,因为你可以看到它很简单,很容易实现ProgressBar而不是ProgressDialog.现在您可以在Handler或ASyncTask中显示/关闭此对话框,它可以满足您的需要

  • 我已将名称更改为正确的 AlertDialog 对象,`show()` / `dismiss()`,但我没有放置 `builder.show()`,因为要关闭它,我们将不得不执行 `dialog.dismiss ()` 这对于喜欢保持代码简单易懂的人来说有点令人困惑,而且因为 `builder.show()` 返回 `AlertDialog` 对象,我们将不得不像 `AlertDialog 对话框一样保存该引用= builder.show();` 然后做 `dialog.dismiss()`,它更容易让 `builder.create()` 在一些 `AlertDialog` 对象中返回,然后使用该对象来显示和隐藏对话框 (2认同)

Han*_*Han 12

是的,不推荐使用ProgressDialog,但不支持Dialog.

您可以将自己的XML文件(包含进度条和加载文本)扩展到对话框对象中,然后使用show()dismiss()函数显示或隐藏它.这是一个例子(Kotlin):

ProgressDialog类:

class ProgressDialog {
companion object {
    fun progressDialog(context: Context): Dialog{
        val dialog = Dialog(context)
        val inflate = LayoutInflater.from(context).inflate(R.layout.progress_dialog, null)
        dialog.setContentView(inflate)
        dialog.setCancelable(false)
        dialog.window!!.setBackgroundDrawable(
                ColorDrawable(Color.TRANSPARENT))
        return dialog
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:background="#fff"
android:padding="13dp"
android:layout_height="wrap_content">
<ProgressBar
    android:id="@+id/progressBar"
    style="?android:attr/progressBarStyle"
    android:layout_width="100dp"
    android:layout_margin="7dp"
    android:layout_height="100dp"
    android:layout_above="@+id/no_jobs_pickups"/>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_margin="7dp"
    android:layout_toEndOf="@+id/progressBar"
    android:text="Loading..." />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

在你的代码中:只是做var dialog = ProgressDialog.progressDialog(context)

显示: dialog.show()

隐藏: dialog.dismiss()


Ram*_*aya 11

好吧,如果你真的想违背他们的意愿,你仍然可以使用Sweet Alert Dialog或自己创建一个.

progress_dialog_layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TableRow
        android:layout_centerInParent="true"
        android:layout_width="match_parent"
        android:layout_height="64dp" >

        <ProgressBar
            android:id="@+id/progressBar2"
            style="?android:attr/progressBarStyle"
            android:layout_width="wrap_content"
            android:layout_height="match_parent" />

        <TextView
            android:gravity="center|left"
            android:id="@+id/textView9"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:textColor="@color/black"
            android:textSize="18sp"
            android:text="Downloading data. Please wait.." />
    </TableRow>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

Java代码:

AlertDialog b;
AlertDialog.Builder dialogBuilder;

public void ShowProgressDialog() {
dialogBuilder = new AlertDialog.Builder(DataDownloadActivity.this);
LayoutInflater inflater = (LayoutInflater) getSystemService( Context.LAYOUT_INFLATER_SERVICE );
            View dialogView = inflater.inflate(R.layout.progress_dialog_layout, null);
            dialogBuilder.setView(dialogView);
            dialogBuilder.setCancelable(false);
            b = dialogBuilder.create();
            b.show();
        }

        public void HideProgressDialog(){

            b.dismiss();
        }
Run Code Online (Sandbox Code Playgroud)

  • 为什么使用库只是为了显示进度对话框? (3认同)
  • 我曾在生产环境中使用SweetAlertDialog经历过糟糕的经历,请谨慎操作,因为它会在窗口之间泄漏。 (2认同)

Ale*_*ano 10

You don't need to import any custom library.

I prefer to use the modern AlertDialog so this is the Kotlin version for the great answer posted by Kishan Donga in this page.

Kotlin code:

fun setProgressDialog(context:Context, message:String):AlertDialog {
    val llPadding = 30
    val ll = LinearLayout(context)
    ll.orientation = LinearLayout.HORIZONTAL
    ll.setPadding(llPadding, llPadding, llPadding, llPadding)
    ll.gravity = Gravity.CENTER
    var llParam = LinearLayout.LayoutParams(
                  LinearLayout.LayoutParams.WRAP_CONTENT,
                  LinearLayout.LayoutParams.WRAP_CONTENT)
    llParam.gravity = Gravity.CENTER
    ll.layoutParams = llParam

    val progressBar = ProgressBar(context)
    progressBar.isIndeterminate = true
    progressBar.setPadding(0, 0, llPadding, 0)
    progressBar.layoutParams = llParam

    llParam = LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT)
    llParam.gravity = Gravity.CENTER
    val tvText = TextView(context)
    tvText.text = message
    tvText.setTextColor(Color.parseColor("#000000"))
    tvText.textSize = 20.toFloat()
    tvText.layoutParams = llParam

    ll.addView(progressBar)
    ll.addView(tvText)

    val builder = AlertDialog.Builder(context)
    builder.setCancelable(true)
    builder.setView(ll)

    val dialog = builder.create()
    val window = dialog.window
    if (window != null) {
        val layoutParams = WindowManager.LayoutParams()
        layoutParams.copyFrom(dialog.window?.attributes)
        layoutParams.width = LinearLayout.LayoutParams.WRAP_CONTENT
        layoutParams.height = LinearLayout.LayoutParams.WRAP_CONTENT
                dialog.window?.attributes = layoutParams
    }
    return dialog
}
Run Code Online (Sandbox Code Playgroud)

Usage:

val dialog = setProgressDialog(this, "Loading..")
dialog.show()
Run Code Online (Sandbox Code Playgroud)

Output:

在此处输入图片说明


Fur*_*qan 6

ProgressBar是ProgressDialog的最佳替代方案.用于指示操作进度的用户界面元素.

有关详细信息,请参阅此Google文档: https ://developer.android.com/reference/android/widget/ProgressBar.html

  • 他说"除了进度条". (12认同)

cre*_*not 5

文档页面所述,替代方法 ProgressBar. ProgressDialog的外观可以通过将 aProgressBar放入AlertDialog.

你仍然可以使用它,但 Android 不希望你使用它,这就是它被弃用的原因。因此,您应该考虑以另一种方式解决您的问题,例如将 a 嵌入ProgressBar到您的Layout.