Android ProgressBar进度条的使用

ProgressBar : 进度条

<ProgressBar/>

step1 activity_main.xml

<?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">

<!--    案例1:点击按钮,显示/隐藏进度条-->
    <ProgressBar
        android:id="@+id/pb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>


    <Button
        android:text="显示/隐藏进度条"
        android:onClick="csClick"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

<!--    案例2:点击按钮,进度加10-->
<!--    水平进度条 :style="?android:attr/progressBarStyleHorizontal-->
    <ProgressBar
        android:id="@+id/longpb"
        android:max="100"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="300dp"
        android:layout_height="wrap_content"/>
    <Button
        android:text="模拟下载"
        android:onClick="loading"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

<!--   案例3: indeterminate="true"就不显示进度条的进度了-->
    <ProgressBar
        android:max="100"
        style="?android:attr/progressBarStyleHorizontal"
        android:indeterminate="true"
        android:layout_width="300dp"
        android:layout_height="wrap_content"/>
</LinearLayout>

step2 MainActivity.java

package com.example.myprogressbar;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.ProgressBar;

public class MainActivity extends AppCompatActivity {

    private ProgressBar pb;
    private ProgressBar longpb;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        pb = findViewById(R.id.pb);
        longpb = findViewById(R.id.longpb);
    }

    //案例1
    public void csClick(View view) {
        //如果进度条隐藏的时候就让它显示
        if(pb.getVisibility() == View.GONE){
            pb.setVisibility(View.VISIBLE);
        }else{
            //如果进度条显示的时候就让它隐藏
            pb.setVisibility(View.GONE);
        }
    }

    //案例2
    public void loading(View view) {
        int progress = longpb.getProgress();
        //每点一下按钮,下载的进度就加10
        progress +=10;
        longpb.setProgress(progress);
    }
}

效果

猜你喜欢

转载自blog.csdn.net/m0_45877477/article/details/124970352#comments_22811528