三種進度條方式:滾動進度條,水平進度條,自定義進度條
廢話不多說,直接上代碼。
xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:android="http://schemas.android.com/apk/res/android" > <!--滾動等待--> <Button android:id="@+id/bu_circle_progress" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="滾動等待進度條"/> <!--水平進度條加載--> <Button android:id="@+id/bu_horizontal_progress" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="水平進度條"/> <!--自定義加載提示框--> <Button android:id="@+id/bu_my_progress" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="自定義提示框"/> </LinearLayout>
java:
public class ProgressDemoActivity extends AppCompatActivity implements View.OnClickListener { //滾動等待 Button circle_progress; //水平進度條 Button horizontal_progress; //自定義加載框 Button my_progress; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_progress_demo); circle_progress=findViewById(R.id.bu_circle_progress); horizontal_progress=findViewById(R.id.bu_horizontal_progress); my_progress=findViewById(R.id.bu_my_progress); } public void onClick(View v) { switch (v.getId()){ case R.id.bu_circle_progress: final ProgressDialog progressDialog= new ProgressDialog(ProgressDemoActivity.this); //設定標題圖片 progressDialog.setIcon(R.drawable.tip1); //設定標題 progressDialog.setTitle("請等待"); //設定內容展示 progressDialog.setMessage("加載中。。。"); //設置不可取消 progressDialog.setCancelable(false); //不要忘記展示進度條 progressDialog.show(); //模擬耗時操作 new Thread(new Runnable() { @Override public void run() { try { //等待5秒 Thread.sleep(5000); }catch (InterruptedException e){ e.printStackTrace(); }finally { //銷毀進度條 progressDialog.dismiss(); } } }).start(); break; case R.id.bu_horizontal_progress: final ProgressDialog progressDialog1= new ProgressDialog(ProgressDemoActivity.this); //設定進度條展示為水平 progressDialog1.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); progressDialog1.setTitle("提示"); progressDialog1.setMessage("加載中..."); progressDialog1.setIcon(R.drawable.tip1); //設置最大值 progressDialog1.setMax(100); //設定進度條默認進度 progressDialog1.setProgress(0); progressDialog1.setCancelable(false); progressDialog1.show(); new Thread(new Runnable() { @Override public void run() { int count=0; while (count<=100) { progressDialog1.setProgress(count++); try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } progressDialog1.dismiss(); } }).start(); break; case R.id.bu_my_progress: final Dialog dialog= DialogUtil.createLoadingDialog(this,"加載中請等待。。。"); dialog.show(); //模擬耗時任務 new Thread(new Runnable() { @Override public void run() { try { Thread.sleep(5000); }catch (InterruptedException e){ e.printStackTrace(); }finally { dialog.dismiss(); } } }).start(); }
自定義的布局需要在layout中根據自己需求來設計
效果展示:
滾動進度條:
水平進度條:
自定義進度條: