Android ProgressBar(进度条)使用

例题2-10

activity_main.xml代码

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <GridLayout android:layout_width="fill_parent"
 3     android:layout_height="fill_parent"
 4     android:columnCount="4"
 5     android:rowCount="6"
 6     xmlns:android="http://schemas.android.com/apk/res/android">
 7     <ProgressBar
 8         android:id="@+id/progressbar1"
 9         android:layout_width="650px"
10         android:layout_height="wrap_content"
11         android:max="200"
12         android:progress="20"
13         android:layout_columnSpan="4"
14         style="@style/Widget.AppCompat.ProgressBar.Horizontal"
15         >
16     </ProgressBar>
17     <Button
18         android:id="@+id/but1"
19         android:layout_width="wrap_content"
20         android:layout_height="wrap_content"
21         android:text="减少"/>
22     <Button
23         android:id="@+id/but2"
24         android:layout_width="wrap_content"
25         android:layout_height="wrap_content"
26         android:text="增加"/>
27 </GridLayout>

Mainactivity.java代码

 1 package com.example.hello;
 2 
 3 import androidx.appcompat.app.AppCompatActivity;
 4 
 5 import android.os.Bundle;
 6 import android.view.View;
 7 import android.widget.Button;
 8 import android.widget.ProgressBar;
 9 
10 public class MainActivity extends AppCompatActivity {
11     Button but1;
12     Button but2;
13     ProgressBar pro;
14     @Override
15     protected void onCreate(Bundle savedInstanceState) {
16         super.onCreate(savedInstanceState);
17         setContentView(R.layout.activity_main);
18         pro = (ProgressBar)findViewById(R.id.progressbar1);
19         but1 = (Button)findViewById(R.id.but1);
20         but2 = (Button)findViewById(R.id.but2);
21         but1.setOnClickListener(new View.OnClickListener() {
22             @Override
23             public void onClick(View v) {
24                 pro.incrementProgressBy(-10);
25             }
26         });
27         but2.setOnClickListener(new View.OnClickListener() {
28             @Override
29             public void onClick(View v) {
30                 pro.incrementProgressBy(10);
31             }
32         });
33     }
34 
35 }
原文地址:https://www.cnblogs.com/xiaowangdatie/p/13725021.html