Android原生控件 -- ProgressBar(进度加载) & ProgressDialog(进度展示对话框)
⒈ProgressBar使用
默认
<ProgressBar android:layout_width="match_parent" android:layout_height="wrap_content" style="@android:style/Widget.Material.ProgressBar"/>
设置是否可见
<ProgressBar android:layout_width="match_parent" android:layout_height="wrap_content" style="@android:style/Widget.Material.ProgressBar" android:visibility="visible"/>
水平的进度条
<ProgressBar android:layout_width="match_parent" android:layout_height="wrap_content" style="@android:style/Widget.ProgressBar.Horizontal"/>
添加总进度、当前进度及二级进度(给人一种缓存的感觉)
<ProgressBar android:layout_width="match_parent" android:layout_height="wrap_content" style="@android:style/Widget.ProgressBar.Horizontal" android:max="100" android:progress="10" android:secondaryProgress="30"/>
模拟进度
package cn.coreqi.helloworld2; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.widget.ProgressBar; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private ProgressBar mPb1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mPb1 = findViewById(R.id.pb1); handler.sendEmptyMessage(0); } Handler handler = new Handler(){ @Override public void handleMessage(@NonNull Message msg) { super.handleMessage(msg); if(mPb1.getProgress() < 100){ handler.postDelayed(runnable,500); }else { Toast.makeText(getApplicationContext(),"加载完成",Toast.LENGTH_LONG).show(); } } }; Runnable runnable = new Runnable() { @Override public void run() { mPb1.setProgress(mPb1.getProgress() + 5); } }; }
设置自定义进度图片旋转
<?xml version="1.0" encoding="utf-8"?> <animated-rotate xmlns:android="http://schemas.android.com/apk/res/android" android:drawable="@drawable/icon_progress" android:pivotX="50%" android:pivotY="50%"> </animated-rotate>
<ProgressBar android:id="@+id/pb1" android:layout_width="match_parent" android:layout_height="wrap_content" android:indeterminateDrawable="@drawable/bg_progress"/>
⒉ProgressDialog使用
普通用法
ProgressDialog progressDialog = new ProgressDialog(getApplicationContext()); progressDialog.setTitle("提示"); progressDialog.setMessage("正在加载"); progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() { @Override public void onCancel(DialogInterface dialog) { } }); //progressDialog.setCancelable(false); progressDialog.show();
进度条用法
ProgressDialog progressDialog = new ProgressDialog(getApplicationContext()); progressDialog.setTitle("提示"); progressDialog.setMessage("正在下载"); progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() { @Override public void onCancel(DialogInterface dialog) { } }); //progressDialog.setCancelable(false); progressDialog.show();
添加按钮
ProgressDialog progressDialog = new ProgressDialog(getApplicationContext()); progressDialog.setTitle("提示"); progressDialog.setMessage("正在下载"); progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() { @Override public void onCancel(DialogInterface dialog) { } }); progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, "后台进行", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); //progressDialog.setCancelable(false); progressDialog.show();