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="wrap_content"
    android:orientation="vertical">
    <ProgressBar
        android:id="@+id/progressbar1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <Button
        android:id="@+id/button1"
        android:text="显示隐藏进度条"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <View
        android:layout_width="match_parent"
        android:layout_height="1px"
        android:background="@color/black"/>
    <ProgressBar
        android:id="@+id/progressbar2"
        style="?android:attr/progressBarStyleHorizontal"
        android:max="100"
        android:layout_width="200dp"
        android:layout_height="wrap_content"/>
    <Button
        android:id="@+id/button2"
        android:text="模拟下载"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>
MainActivity.java
package com.example.myprogressbar;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
public class MainActivity extends AppCompatActivity {
    private static final String TAG = "Fengxz";
    private ProgressBar progress1;
    private ProgressBar progress2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        progress1 = findViewById(R.id.progressbar1);
        progress2 = findViewById(R.id.progressbar2);
        Button button1 = findViewById(R.id.button1);
        Button button2 = findViewById(R.id.button2);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.e(TAG, "显示隐藏进度条 ");
                if (progress1.getVisibility() == View.GONE) {
                    progress1.setVisibility(View.VISIBLE);
                }else {
                    progress1.setVisibility(View.GONE);
                }
            }
        });
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.e(TAG, "模拟下载 ");
                int progress = progress2.getProgress();
                progress += 10;
                progress2.setProgress(progress);
            }
        });
    }
}
原创文章,作者:6024010,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/272044.html
