-------------
MainActivity.java
--------------
package com.example.gby.s01_e17_progressbar;
import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ProgressBar;
public class MainActivity extends AppCompatActivity {
private ProgressBar progressBar;
private Button firstButton;
private Button secondButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar = (ProgressBar)findViewById(R.id.firstProgressBar);
firstButton = (Button)findViewById(R.id.firstButton);
secondButton = (Button)findViewById(R.id.secondButton);
progressBar.setMax(100);
firstButton.setOnClickListener(new FirstListener());
secondButton.setOnClickListener(new SecondListener());
}
class FirstListener implements View.OnClickListener{
@Override
public void onClick(View view) {
progressBar.incrementProgressBy(10);
}
}
class SecondListener implements View.OnClickListener{
@Override
public void onClick(View view) {
progressBar.incrementSecondaryProgressBy(20);
}
}
}
---------------
activity_main.xml
----------------
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.gby.s01_e17_progressbar.MainActivity">
<ProgressBar
android:id="@+id/firstProgressBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleHorizontal"
/>
<Button
android:id="@+id/firstButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/firstProgressBar"
android:text="增加第一进度"
/>
<Button
android:id="@+id/secondButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/firstButton"
android:text="增加第二进度"
/>
</RelativeLayout>