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

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

当前回答

您可以简单地为progressbar设计一个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>

显示进度对话框的代码代码。只需复制这段代码并粘贴它的片段。

Dialog dialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setView(R.layout.progress);
        // This should be called once in your Fragment's onViewCreated() or in Activity onCreate() method to avoid dialog duplicates.
        dialog = builder.create();
    }
    
//   This method is used to control the progress dialog.
      private void setDialog(boolean show){
               if (show)dialog.show();
                    else dialog.dismiss();
           }

然后只要在你想要显示progressdialog时调用这个方法,并将true作为参数来显示它,或将false作为参数来取消对话框。

其他回答

是的,在API级别26中已弃用。相反,您可以使用progressBar。

以编程方式创建:

首先获取根布局的引用

RelativeLayout layout = findViewById(R.id.display);  //specify here Root layout Id

or

RelativeLayout layout = findViewById(this);

然后添加进度条

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);

显示进度条

progressBar.setVisibility(View.VISIBLE);

隐藏进度条

progressBar.setVisibility(View.GONE);

要禁用用户交互,您只需要添加以下内容 代码

getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
                           WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);

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

getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);

只是为了将来参考,将android. r.t r. progressbarstylesmall更改为android. r.t r. progressbarstylehorizontal。

下面的代码只能在API级别21以上工作

progressBar.setProgressTintList(ColorStateList.valueOf(Color.RED));

通过xml创建:

<ProgressBar
        android:id="@+id/progressbar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:indeterminate="true"
        android:max="100"
        android:backgroundTint="@color/white"
        android:layout_below="@+id/framelauout"
        android:indeterminateTint="#1a09d6"
        android:layout_marginTop="-7dp"/>

在你的活动中

progressBar = (ProgressBar) findViewById(R.id.progressbar);

显示/隐藏进度条是一样的

 progressBar.setVisibility(View.VISIBLE); // To show the ProgressBar 
 progressBar.setVisibility(View.INVISIBLE); // To hide the ProgressBar

下面是它的样例图像:

详情如下: 1. 引用一个 2. 引用两个

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

我使用来自https://github.com/Q115/DelayedProgressDialog的DelayedProgressDialog,它与ProgressDialog相同,在必要时具有延迟的额外好处。

使用它类似于Android O之前的ProgressDialog:

DelayedProgressDialog progressDialog = new DelayedProgressDialog();
progressDialog.show(getSupportFragmentManager(), "tag");

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

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()

它可能对其他人有帮助。

许多流行的应用程序都有不同的方法来显示任何事情的进展,如网络请求,文件加载等。加载旋转器不会显示已加载或剩余的内容有多少。从UI/UX的角度来看,有一段时间的不确定性是不好的。许多流行的应用程序(Facebook, Linkedin等)通过首先显示基本的UI显示来解决这个问题。然后加载的内容逐渐填充在屏幕上。

我已经在我的应用程序中使用了shimmer来解决这个问题。

有一篇关于这方面的好文章,它将对其他人有益