public class MainActivity extends Activity implements View.OnClickListener{
private ProgressBar progress;
private Button add;
private Button reduce;
private Button reset;
private Button show;
private TextView text;
private ProgressDialog prodig;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//注册控件
init();
add.setOnClickListener(this);
reduce.setOnClickListener(this);
reset.setOnClickListener(this);
show.setOnClickListener(this);
}
private void init() {
progress = (ProgressBar)findViewById(R.id.progressBar);
add = (Button) findViewById(R.id.button1);
reduce = (Button) findViewById(R.id.button2);
reset =(Button)findViewById(R.id.button3);
text =(TextView)findViewById(R.id.textView);
show =(Button)findViewById(R.id.show);
//获取进度
int first = progress.getProgress();
int second= progress.getSecondaryProgress();
int Max = progress.getMax();
text.setText("第一进度条百分比:" + ((int) (first / (float) Max * 100)) + "第二进度条百分比为:" + ((int) (second / (float) Max * 100)));
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button1:
progress.incrementProgressBy(10);
progress.incrementSecondaryProgressBy(10);
break;
case R.id.button2:
progress.incrementProgressBy(-10);
progress.incrementSecondaryProgressBy(-10);
break;
case R.id.button3:
progress.setProgress(0);
progress.setSecondaryProgress(10);
break;
case R.id.show:
//新建ProgressDialog对象
prodig = new ProgressDialog(MainActivity.this);
//设置ProgressDialog显示风格
prodig.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
//设置ProgressDialog标题
prodig.setTitle("慕尼黑");
//设置ProgressDialog文本信息
prodig.setMessage("欢迎大家支持慕课网!");
//设置ProgressDialog图标
prodig.setIcon(R.mipmap.ic_launcher);
/*
设置ProgressBar进度条属性
*/
//设定最大进度条
prodig.setMax(100);
//设定初始化进度
prodig.incrementProgressBy(50);
//进度条明确显示进度 设置为ture就会来回滚动 表示在运行
prodig.setIndeterminate(false);
/*
设定一个确定按钮
*/
prodig.setButton(DialogInterface.BUTTON_POSITIVE,"确定可好?", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"今天天气萌萌哒",Toast.LENGTH_SHORT).show();
}
});
//点击ProgressDialog区域来退出
prodig.setCancelable(true);
//显示ProgressDialog
prodig.show();
break;
}
text.setText("第一进度条百分比:" + ((int) (progress.getProgress() / (float) progress.getMax() * 100)) + "第二进度条百分比为:" + ((int) (progress.getSecondaryProgress() / (float) progress.getMax() * 100)));
}
注意:
1.进度条明确显示进度 设置为ture就会来回滚动 表示在运行
prodig.setIndeterminate(false);
prodig.incrementProgressBy(50);
2.ProgressDialog进度条 设置初始值时
在show()之前 只能用
prodig.incrementProgressBy(50);
show()之后
才能用prodig.setProgress(50);
原文:http://www.cnblogs.com/zmaibbs7/p/4844142.html