我发现ProgressDialog现在已经被弃用了。除了ProgressBar,还有什么替代工具可以代替它。 我使用的是android studio 2.3.3版。

ProgressDialog progressDialog=new ProgressDialog(this);
progressDialog.show();

当前回答

下面是@Han建议的ProgressDialog类的kotlin版本

class ProgressDialog(context: Context) : Dialog(context) {
init {
    val view = View.inflate(context, R.layout.progress_dialog, null)
    setContentView(view)
    setCancelable(false)
    window?.setBackgroundDrawable(
        ColorDrawable(Color.TRANSPARENT)
    )
}

}

其他回答

在进度对话框中,用户不能做任何工作。所有后台进程在进度对话框中停止。因此,建议使用进度条代替进度对话框。

ProgressDialog在API级别26中已弃用。

“已弃用”指的是正在被更新的函数或元素替换的函数或元素。

ProgressDialog是一个模态对话框,它阻止用户与之交互 而不是使用这个类,你应该使用一个进度 比如ProgressBar,它可以嵌入到你的应用程序的 UI。

优势

我个人认为ProgressBar比这两者更有优势。ProgressBar是一个指示操作进度的用户界面元素。以非中断的方式向用户显示进度条。在应用程序的用户界面中显示进度条。

下面是我的不确定进度对话框版本:

layout_loading_dialog.xml:

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

    <ProgressBar
        android:id="@+id/progressBar"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="10dp"
        android:layout_marginRight="10dp"
        android:layout_weight="1" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="4"
        android:gravity="center"
        android:textAlignment="textStart"
        android:id="@+id/message"
        tools:text="Please wait..." />
</LinearLayout>

IndeterminateProgressDialog.kt:

class IndeterminateProgressDialog(context: Context) : AlertDialog(context) {
    private val messageTextView: TextView

    init {
        val view = LayoutInflater.from(context).inflate(R.layout.layout_loading_dialog, null)
        messageTextView = view.findViewById(R.id.message)
        setView(view)
    }

    override fun setMessage(message: CharSequence?) {
        this.messageTextView.text = message.toString()
    }

}

用法:

   val dialog = IndeterminateProgressDialog(context)
                    dialog.setMessage("Please wait...")
                    dialog.setCanceledOnTouchOutside(false)
                    dialog.setCancelable(false)
                    dialog.show()

是的,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
    }
  }
}

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"/>
<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>

在你的代码中: 只需执行var dialog = progressdialog。progressdialog (context)

显示方法:dialog.show()

隐藏方法:dialog.dismiss()

正如文档页中提到的,替代方案是ProgressBar。ProgressDialog的外观可以通过在AlertDialog中放置ProgressBar来复制。

你仍然可以使用它,但Android不希望你使用它,这就是它被弃用的原因。所以你应该考虑用另一种方式解决你的问题,比如在你的布局中嵌入一个ProgressBar。