Android study notes (Android Studio) 3-3 (ProgressBar & ProgressDialog) (loading progress bar, going round in circles) pop-up component of the UI components

Android Study Notes 3-3


Recommended for newbies to learn video: B station https://www.bilibili.com/video/av38409964 point I transfer


3-3 ProgressBar & ProgressDialog (loading progress bar, going round in circles)

Today, under a program called ScreenToGif software that can intercept a moving map, la la la

  • Some properties are not shown

    • android: secondaryProgress = "30" progress bar 30 is provided two
    • android: indeterminateDrawable = "@ drawable / bg_progress" Setting custom styles
  • activity_progress.xml file

      <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical"
          android:gravity="center_horizontal"
          android:padding="15dp">
      
          <ProgressBar
              android:id="@+id/pb1"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              />
      
          <ProgressBar
              android:id="@+id/pb2"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              style="@android:style/Widget.ProgressBar"
              android:layout_marginTop="10dp"/>
      
          <ProgressBar
              android:id="@+id/pb3"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              style="@android:style/Widget.ProgressBar.Horizontal"
              android:layout_marginTop="10dp"
              android:max="100"
              android:progress="10"
              android:secondaryProgress="30"/>
      
          <ProgressBar
              android:id="@+id/pb4"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              style="@android:style/Widget.Material.ProgressBar.Horizontal"
              android:layout_marginTop="10dp"
              android:max="100"
              android:progress="10"
              android:secondaryProgress="30"/>
      
          <Button
              android:id="@+id/btn_start"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:text="模拟进度条"
              android:layout_marginTop="10dp"/>
      
          <ProgressBar
              android:id="@+id/pb5"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              style="@style/MyProgressBar"  自定义效果
              android:layout_marginTop="10dp"/>
      
          <Button
              android:id="@+id/btn_progress_dialog1"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:text="ProgressDialog1"
              android:textAllCaps="false"
              android:layout_marginTop="10dp"/>
      
          <Button
              android:id="@+id/btn_progress_dialog2"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:text="ProgressDialog2"
              android:textAllCaps="false"
              android:layout_marginTop="10dp"/>
      
      </LinearLayout>
    
  • effect
    Here Insert Picture Description

  • Custom layout bg_progress.xml files in the drawable

      <?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%">
      <!--两个50%确定图片的旋转中心-->
      </animated-rotate>
    
  • styles.xml file to add custom styles

      <style name="MyProgressBar">
      	<item name="android:indeterminateDrawable">@drawable/bg_progress</item>
      </style>
    
  • ProgressActivity.java file

      package com.ylw.helloworld;
      
      import androidx.annotation.NonNull;
      import androidx.appcompat.app.AppCompatActivity;
      
      import android.app.ProgressDialog;
      import android.content.DialogInterface;
      import android.os.Bundle;
      import android.os.Handler;
      import android.os.Message;
      import android.view.View;
      import android.widget.Button;
      import android.widget.ProgressBar;
      
      import com.ylw.helloworld.util.ToastUtil;
      
      public class ProgressActivity extends AppCompatActivity {
      
          private ProgressBar mPb3;
          private Button mBtnStart,mBtnProgressDialog1,mBtnProgressDialog2;
      
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_progress);
              mPb3 = findViewById(R.id.pb3);
              mBtnStart = findViewById(R.id.btn_start);
              mBtnProgressDialog1 = findViewById(R.id.btn_progress_dialog1);
              mBtnProgressDialog2 = findViewById(R.id.btn_progress_dialog2);
      
              mPb3.setProgress(30);   //设置进度条到30
              mBtnStart.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      handler.sendEmptyMessage(0);
                  }
              });
              mBtnProgressDialog1.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      //虽然标了中划线但还是可以用的
                      ProgressDialog progressDialog = new ProgressDialog(ProgressActivity.this);
                      progressDialog.setTitle("提示");
                      progressDialog.setMessage("正在加载");
                      progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() { //监听取消
                          @Override
                          public void onCancel(DialogInterface dialog) {
                              ToastUtil.showMsg(ProgressActivity.this,"cancel...");
                          }
                      });
                      progressDialog.setCancelable(false);    //设置不可以被取消
                      progressDialog.show();
                  }
              });
              mBtnProgressDialog2.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      //虽然标了中划线但还是可以用的
                      ProgressDialog progressDialog = new ProgressDialog(ProgressActivity.this);
                      progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);   //设置样式为进度条
                      progressDialog.setTitle("提示");
                      progressDialog.setMessage("正在下载...");
                      //设置按钮和点击事件
                      progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, "等一会", new DialogInterface.OnClickListener() {
                          @Override
                          public void onClick(DialogInterface dialog, int which) {
                              //
                          }
                      });
                      progressDialog.show();
                  }
              });
          }
      
          Handler handler = new Handler(){    //容器
              @Override
              public void handleMessage(@NonNull Message msg) {
                  super.handleMessage(msg);
                  if (mPb3.getProgress()<100){    //如果进度小于100
                      handler.postDelayed(runnable,500);   //延迟500毫秒发出消息
                  }else{
                      ToastUtil.showMsg(ProgressActivity.this,"加载完成");
                  }
              }
          };
      
          Runnable runnable = new Runnable() {    //线程
              @Override
              public void run() {
                  mPb3.setProgress(mPb3.getProgress()+5); //把当前进度+5
                  handler.sendEmptyMessage(0);
              }
          };
      }
    
  • Effect (today under a software called ScreenToGif, you can intercept a moving map, la la la)
    Here Insert Picture Description

  • Simulation progress bar effect
    Here Insert Picture Description

  • ProgressDialog1 effect
    Here Insert Picture Description

  • ProgressDialog2 effect
    Here Insert Picture Description

Published 81 original articles · won praise 29 · views 5230

Guess you like

Origin blog.csdn.net/qq_43594119/article/details/104579878