第一步: 布局文件中引入ProgressBar
<?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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.svse.progressbar.MainActivity">
<ProgressBar
android:id="@+id/progress"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="37dp"
android:max="100"
android:progress="50"
android:secondaryProgress="60" />
<!--
android:max="100" 表示最大刻度
android:progress="50" 表示第一进度
android:secondaryProgress="60" 表示第二进度
-->
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/progress"
android:layout_marginLeft="22dp"
android:layout_marginStart="22dp"
android:layout_marginTop="15dp"
android:text="TextView" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/textView"
android:layout_marginTop="20dp"
android:text="@string/add" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/button"
android:layout_marginLeft="21dp"
android:layout_marginStart="21dp"
android:layout_toEndOf="@+id/button"
android:layout_toRightOf="@+id/button"
android:text="@string/reduce" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/button2"
android:layout_marginLeft="27dp"
android:layout_marginStart="27dp"
android:layout_toEndOf="@+id/button2"
android:layout_toRightOf="@+id/button2"
android:text="@string/reset" />
<Button
android:text="@string/dialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="24dp"
android:id="@+id/button4" />
</RelativeLayout>
第二步: 编写java代码
package com.svse.progressbar;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private ProgressBar progressBar;
private Button b1, b2, b3 ,b4;
private TextView textView;
//声明对话框进度条对象
private ProgressDialog pd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
progressBar = (ProgressBar) findViewById(R.id.progress);
b1 = (Button) findViewById(R.id.button);
b2 = (Button) findViewById(R.id.button2);
b3 = (Button) findViewById(R.id.button3);
b4 = (Button) findViewById(R.id.button4);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
b3.setOnClickListener(this);
b4.setOnClickListener(this);
textView = (TextView) findViewById(R.id.textView);
//得到进度条的最大刻度
int max = progressBar.getMax();
//得到进度条的第一进度
int first = progressBar.getProgress();
//得到进度条的第二进度
int second = progressBar.getSecondaryProgress();
/**
* 第一进度(第二进度)除以总进度(小int 除以 大int得到的值永远是0,除非进度条满,此时值为1)
* 需要把总进度转化为float类型,乘以100后再转化为int类型
*/
textView.setText("第一进度条为" + (int) (first / (float) max * 100)
+ "% 第二进度条为" + (int) (second / (float) max * 100) + "%");
}
@Override
public void onClick(View v) {
switch (v.getId()) {
//点击进度条增加
case R.id.button:
progressBar.incrementProgressBy(10);
progressBar.incrementSecondaryProgressBy(10);
break;
//点击进度条减少
case R.id.button2:
progressBar.incrementProgressBy(-10);
progressBar.incrementSecondaryProgressBy(-10);
break;
//点击进度条重置
case R.id.button3:
progressBar.setProgress(50);
progressBar.setSecondaryProgress(60);
break;
//点击以对话框形式显示进度条
case R.id.button4:
pd = new ProgressDialog(MainActivity.this);
//设置对话框进度条的显示风格
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
//设置标题
pd.setTitle("标题");
//设置显示信息
pd.setMessage("显示信息");
//设置图标
pd.setIcon(R.mipmap.ic_launcher);
//设定ProgressDialog的属性
//最大刻度
pd.setMax(100);
//初始化进度
pd.incrementProgressBy(50);
//进度条明确显示进度
pd.setIndeterminate(false);
//设置对话框的确定按钮
pd.setButton(DialogInterface.BUTTON_POSITIVE,"确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "您点击了确定", Toast.LENGTH_SHORT).show();
}
});
pd.setButton(DialogInterface.BUTTON_NEGATIVE,"取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "您点击了取消", Toast.LENGTH_SHORT).show();
}
});
//是否可以通过返回按钮退出对话框
pd.setCancelable(true);
//显示ProgressDialog
pd.show();
break;
}
textView.setText("第一进度条为" + (int) (progressBar.getProgress() / (float) progressBar.getMax() * 100)
+ "% 第二进度条为" + (int) (progressBar.getSecondaryProgress() / (float) progressBar.getMax() * 100) + "%");
}
}