Android中的进度条有多种:对话框进度条,标题栏进度条和水平进度条
1.对话框进度条
a) 覆盖Activity的onCreateDialog()方法,并在其中创建对话框。
b) 调用Activity的showDialog()方法,显示进度条对话框。
public class MainActivity extends Activity { private Button myBtn; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); myBtn = (Button)findViewById(R.id.Button01); myBtn.setOnClickListener(new OnClickListener() { public void onClick(View v) { showDialog(0); } }); } @Override protected Dialog onCreateDialog(int id) { ProgressDialog dialog = new ProgressDialog(this); // 可以不显示标题 dialog.setTitle("测试对话框"); dialog.setIndeterminate(true); dialog.setMessage("程序正在加载请稍后!"); dialog.setCancelable(true); dialog.setButton(Dialog.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } } ); return dialog; } }
2.标题进度条
a) 调用Activity的requestWindowFeature()方法,获得进度条。
b) 调用Activity的setProgressBarIndeterminateVisibility()方法显示进度条
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); b1 = (Button)findViewById(R.id.Button01); b1.setOnClickListener(new OnClickListener() { public void onClick(View v) { setProgressBarIndeterminateVisibility(true); } });
3.水平进度条
a) 在布局文件中声明ProgressBar
b) 在Activiy中获取ProgressBar的实例
c) 调用ProgressBar的incrementProgressBy()方法增加或者减少进度。
progressBar = (ProgressBar)findViewById(R.id.ProgressBar01); b1.setOnClickListener(new OnClickListener() { public void onClick(View v) { progressBar.incrementProgressBy(1); }}); b2.setOnClickListener(new OnClickListener() { public void onClick(View v) { progressBar.incrementProgressBy(-1); }});