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="match_parent"
android:orientation="vertical" >
<SeekBar
android:id="@+id/progressBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:progress="20"
android:secondaryProgress="40" />
<Button
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/add"/>
<Button
android:id="@+id/reduce"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/reduce"/>
<Button
android:id="@+id/reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/reset"/>
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/show"/>
</LinearLayout>
MainActivity.java
package com.example.progressbardemo;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener{
private ProgressBar pb;
private Button add;
private Button reduce;
private Button reset;
private TextView text;
private Button show;
private ProgressDialog pd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
pb = (ProgressBar) findViewById(R.id.progressBar1);
add = (Button) findViewById(R.id.add);
reduce = (Button) findViewById(R.id.reduce);
reset = (Button) findViewById(R.id.reset);
text = (TextView) findViewById(R.id.text);
show = (Button) findViewById(R.id.show);
int firstPb = pb.getProgress();
int secondPb = pb.getSecondaryProgress();
int maxPb = pb.getMax();
text.setText("第一进度百分比:"+(int)(firstPb/(float)maxPb*100)
+"%;第二进度百分比:"+(int)(secondPb/(float)maxPb*100)+"%");
add.setOnClickListener(this);
reduce.setOnClickListener(this);
reset.setOnClickListener(this);
show.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.add:
//第一进度和第二进度各增加10
pb.incrementProgressBy(10);
pb.incrementSecondaryProgressBy(10);
break;
case R.id.reduce:
//第一进度和第二进度各减少10
pb.incrementProgressBy(-10);
pb.incrementSecondaryProgressBy(-10);
break;
case R.id.reset:
pb.setProgress(20);
pb.setSecondaryProgress(40);
break;
case R.id.show:
/**
* 页面显示风格
*/
//新建ProgressDialog对象
pd = new ProgressDialog(MainActivity.this);
//设置显示风格
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
//设置标题
pd.setTitle("胖子");
//设置对话框里的文字信息
pd.setMessage("奔跑吧,胖子!!!");
//设置图标
pd.setIcon(R.drawable.ic_launcher);
/**
* 设定关于ProgressBar的一些属性
*/
//设定最大进度
pd.setMax(100);
//设定初始化已经增长到的进度
pd.incrementProgressBy(20);
//进度条是明确显示进度的
pd.setIndeterminate(false);
/**
* 设定一个确定按钮
*/
pd.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "奔跑吧,胖子!!!", Toast.LENGTH_SHORT).show();
}
});
//是否可以通过返回按钮退出对话框
pd.setCancelable(true);
//显示ProgressDialog
pd.show();
break;
default:
break;
}
}
}