一、运行效果:
二、MainActivity.java:
package com.example.a027_progressdialogapp; import androidx.appcompat.app.AppCompatActivity; import android.app.ProgressDialog; import android.content.DialogInterface; import android.os.Bundle; import android.os.Handler; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity { private Button button,button2; private ProgressDialog progressDialog,progressDialog1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = findViewById(R.id.button); button2 = findViewById(R.id.button2); button2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { progressDialog1 = new ProgressDialog(MainActivity.this); progressDialog1.setProgressStyle(ProgressDialog.STYLE_SPINNER); progressDialog1.setCancelable(false); progressDialog1.setIcon(R.drawable.as8); progressDialog1.setTitle("hello"); progressDialog1.setMax(100); // progressDialog1.setButton(DialogInterface.BUTTON_POSITIVE,"确定", // new DialogInterface.OnClickListener(){ // @Override // public void onClick(DialogInterface progressDialog, int which) { // // } // }); progressDialog1.setButton(DialogInterface.BUTTON_NEGATIVE,"取消", new DialogInterface.OnClickListener(){ @Override public void onClick(DialogInterface progressDialog, int which) { } }); progressDialog1.setMessage("加载中..."); progressDialog1.show(); } }); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { progressDialog = new ProgressDialog(MainActivity.this); progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); progressDialog.setCancelable(false); progressDialog.setIcon(R.drawable.as7); progressDialog.setTitle("提示"); progressDialog.setMax(6000); // progressDialog.setButton(DialogInterface.BUTTON_POSITIVE,"确定", // new DialogInterface.OnClickListener(){ // @Override // public void onClick(DialogInterface progressDialog, int which) { // // } // }); // progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE,"取消", // new DialogInterface.OnClickListener(){ // @Override // public void onClick(DialogInterface progressDialog, int which) { // // } // }); progressDialog.setMessage("装载中..."); progressDialog.show(); new Thread(new Runnable() { @Override public void run() { try { for(int i = 0;i < progressDialog.getMax();i += 100){ progressDialog.setProgress(i); Thread.sleep(300); } } catch (InterruptedException e) { e.printStackTrace(); } progressDialog.dismiss(); } }).start(); } }); } }
三、activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_margin="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:text="水平进度条" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="圆形进度条"
android:layout_margin="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
四、工程源码: