Android开发 PorgressBar(进度条)的使用

圆环进度条(默认)和水平进度条:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical">
    <!-- 圆环形进度条-->
    <ProgressBar
        android:id="@+id/pbCircle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <!-- 水平进度条-->
    <ProgressBar
        android:id="@+id/pbHorizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="@style/Widget.AppCompat.ProgressBar.Horizontal"/>

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="点我" />

</LinearLayout>

设置和获取水平进度条进度:

package com.example.jiandanduihuakuang;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private ProgressBar pbHorizontal;
    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化控件
        pbHorizontal = findViewById(R.id.pbCircle);
        button = findViewById(R.id.button);
        //按钮监听器
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //setProgress()设置进度条进度,getProgress获取进度条进度
                pbHorizontal.setProgress(pbHorizontal.getProgress()+10);
            }
        });
    }


}

猜你喜欢

转载自blog.csdn.net/weixin_44893902/article/details/111768508
今日推荐