//progress的关键属性
//style="?android:attr/progressBarStyleSmall" 设置圈圈的大小。默认大小为中
//android:max=100 最大进度
//android:progress 第一显示进度
//android:secondaryprogress 设置第二进度
//android:indeterminate 设置是否精确显示,false为精确显示。
// progress的方法
//setProgress(int) 设置第一进度,也可在xml直接声明
//setSecondaryProgress(int) 设置第二进度,也可在xml直接声明
//getProgress() 获取第一进度
//getSecondaryProgress() 获取第二进度
//incrementProgressBy(int) 增加或者减少第一进度
//incrementSecondaryProgressBy(int) 增加或者减少第二进度
//getMax() 获取最大进度
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import android.os.Build;
public class MainActivity extends Activity implements OnClickListener {
private ProgressBar progress;
private Button add;
private Button reduce;
private Button reset;
private TextView text;
private ProgressDialog prodialog;//用对话框显示进度条
private Button show;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 启用窗口特征,启用带进度和不带进度的进度条
requestWindowFeature(Window.FEATURE_PROGRESS);//在标题栏显示进度条,必要步骤
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);//在标题栏显示圈圈进度
setContentView(R.layout.main);
// 两种不同的进度条,返boolean类型
setProgressBarVisibility(true);//显示带进度的进度条,即横长条
setProgressBarIndeterminateVisibility(true);//不带进度的进度条,就是一直转的圆圈,true时在标题上显示。可以在加载完毕后返回false,以关闭这个显示。
// 默认最大值Max=10000
setProgress(6666);//给带进度条的进度赋值
init();
}
private void init() {
// TODO Auto-generated method stub
progress = (ProgressBar) findViewById(R.id.horiz);
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);
// 获取第一进度条的进度,已在xml文件中ProgressBar声明
int first = progress.getProgress();
// 获取第二进度条的进度,已在xml文件中ProgressBar声明
int second = progress.getSecondaryProgress();
// 获取进度条的最大进度,已在xml文件中ProgressBar声明
int max = progress.getMax();
//用int除去余数
text.setText("第一进度百分比:" + (int) (first / (float) max * 100)
+ "% 第二进度百分比:" + (int) (second / (float) max * 100) + "%");
add.setOnClickListener(this);//建立点击事件
reduce.setOnClickListener(this);//建立点击事件
reset.setOnClickListener(this);//建立点击事件
show.setOnClickListener(this);//建立点击事件
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {//这个view v中存了各个已经建立点击事件的view。用v.getId()根据你点击的button
case R.id.add: {
// 增加第一进度和第二进度10个刻度
progress.incrementProgressBy(10);
progress.incrementSecondaryProgressBy(10);
break;
}
case R.id.reduce: {
// 减少第一进度和第二进度10个刻度
progress.incrementProgressBy(-10);
progress.incrementSecondaryProgressBy(-10);
break;
}
case R.id.reset: {//重置就重新设置一下原本进度条的初始值
progress.setProgress(50);
progress.setSecondaryProgress(80);
break;
}
case R.id.show://这是一个用对话框显示进度条的页面
{
//新建ProgressDialog对象
prodialog=new ProgressDialog(MainActivity.this);
//设置显示风格 这里用横向进度条显示
prodialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
//设置对话框标题
prodialog.setTitle("啊");
//设置对话框里的文字信息
prodialog.setMessage("呵呵呵");
//设置标题图标
prodialog.setIcon(R.drawable.ic_launcher);
//设定最大进度
prodialog.setMax(100);
//设定初始化已经增长到的进度,下面默认初始化已进入一半
prodialog.incrementProgressBy(50);
//指定进度条明确显示进度
prodialog.setIndeterminate(false);
/*
可以给对话框设置按钮确定,并为它设置点击事件
prodialog.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();
}
});
*/
//是否可以通过返回按钮退出对话框,flse为不能通过返回按钮退出对话框
prodialog.setCancelable(false);
//最后建立显示ProgressDialog
prodialog.show();
break;
}
}
text.setText("第一进度百分比:"+ (int) (progress.getProgress() / (float) progress.getMax() * 100)+ "% 第二进度百分比:"+ (int) (progress.getSecondaryProgress()/ (float) progress.getMax() * 100) + "%");
}
}