ProgressBar

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_width="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">

<ProgressBar
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/pb"
    style="@style/Widget.AppCompat.ProgressBar.Horizontal"
    android:max="100"
    android:progress="20"
    android:secondaryProgress="30"
    />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btn1"
        android:text="add"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btn2"
        android:text="减少"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btn3"
        android:text="重置"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tv_show"/>


</LinearLayout>
package com.example.administrator.hello;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;



public class MainActivity extends Activity {
    private Button btn1,btn2,btn3;
    private ProgressBar progressBar;
    private TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
      btn1=(Button)findViewById(R.id.btn1);
        btn2=(Button)findViewById(R.id.btn2);
        btn3=(Button)findViewById(R.id.btn3);
        progressBar=(ProgressBar)findViewById(R.id.pb);
        textView=(TextView)findViewById(R.id.tv_show);

        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                progressBar.incrementProgressBy(20);
                progressBar.incrementSecondaryProgressBy(20);
                textView.setText("百分比: "+progressBar.getProgress()/(float)progressBar.getMax()*100+"%"+"第二百分比: "+progressBar.getSecondaryProgress()/(float)progressBar.getMax()*100+"%");

            }
        });


        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                progressBar.incrementProgressBy(-20);
                progressBar.incrementSecondaryProgressBy(-20);
                textView.setText("百分比: "+progressBar.getProgress()/(float)progressBar.getMax()*100+"%"+"第二百分比: "+progressBar.getSecondaryProgress()/(float)progressBar.getMax()*100+"%");

            }
        });

        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                progressBar.setProgress(20);
                progressBar.setSecondaryProgress(30);
                textView.setText("百分比: "+progressBar.getProgress()/(float)progressBar.getMax()*100+"%"+"第二百分比: "+progressBar.getSecondaryProgress()/(float)progressBar.getMax()*100+"%");

            }
        });


    }
}
原文地址:https://www.cnblogs.com/excellencesy/p/9070598.html