显示操作进度的对话框
1、使用上一篇创建的同一项目,在activity_main.xml文件中添加一个Button:
<Button android:id="@+id/btn_dialog3" android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="onClick3" android:text="Click to display a detailed progress dialog" />
2、在MainActivity.java文件中添加具体的进度条实现代码:首先添加onClick3()方法:
public void onClick3(View v) { showDialog(1);// id为1,在调用回调方法onCreateDialog()时,将id传进去,使其选择case 1情况。 progressDialog.setProgress(0);// 从0开始 new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub for (int i = 1; i <= 10; i++) { try { Thread.sleep(1000); progressDialog.incrementProgressBy(100 / 10);// 步进为10 } catch (InterruptedException e) { // TODO: handle exception e.printStackTrace(); } } progressDialog.dismiss();// 销毁对话框 } }).start(); }再在onCreateDialog()回调方法中添加id=1时的代码(即实现显示操作进度对话框的代码):case 1: progressDialog = new ProgressDialog(this); progressDialog.setIcon(R.drawable.ic_launcher); progressDialog.setTitle("Downloading files..."); progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);// 设置进度条样式 progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK",// 设置OK按钮 new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub Toast.makeText(getBaseContext(), "OK clicked!", Toast.LENGTH_SHORT).show(); } }); progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel",// 设置取消按钮 new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub Toast.makeText(getBaseContext(), "Cancel clicked!", Toast.LENGTH_SHORT).show(); } }); return progressDialog;
3、运行一下,效果如下:
点击此处下载完整代码~