ProgressDialog==>进度条对话框
ProgressDialog本身就代表一个进度条对话框,程序只需要创建ProgressDialog实例,并将其显示出来就是一个进度条对话框;开发者也可以设置进度对话框里进度条的方法。
包含了如下常用方法:
1.setIndeterminate(boolean indeterminate):设置对话框里的进度条是否显示进度值;
2.setMax(int max):设置对话框里进度条的最大值;
3.setMessage(CharSequence message):设置对话框里显示的消息;
4.setProgress(int value):设置对话框里进度条的进度值;
5.setProgressStyle(int style):设置对话框里进度条的风格;
实例如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 | 布局文件==》 <?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" > <EditText android:id= "@+id/edtContent" android:layout_width= "match_parent" android:layout_height= "wrap_content" /> <Button android:id= "@+id/btnexec" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_gravity= "center" android:text= "progressDialog" /> </LinearLayout> 代码实现==》 package com.example.myprogressdialog1; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.app.Activity; import android.app.Dialog; import android.app.ProgressDialog; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { private int [] date = new int [100]; private int hasDate = 0; final int progress_Dialog = 0x112; private int progressStatus = 0; private ProgressDialog pd; private Handler handler; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btnexec = (Button) this .findViewById(R.id.btnexec); btnexec.setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { showDialog(progress_Dialog); } }); handler = new Handler() { @Override public void handleMessage(Message msg) { if (msg.what == 0x112) { pd.setProgress(progressStatus); } } }; } @Override protected Dialog onCreateDialog( int id, Bundle args) { System. out .println( "---------------create---------------" ); switch (id) { case progress_Dialog: pd = new ProgressDialog( this ); pd.setMax(100); pd.setTitle( "任务完成百分比" ); pd.setMessage( "耗时任务完成百分比" ); // 设置对话框取消按钮不可用 pd.setCancelable( false ); // 设置对话框进度条样式风格 pd.setProgressStyle(ProgressDialog.STYLE_SPINNER); pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); // 设置对话框的进度条是否显示进度 pd.setIndeterminate( true ); break ; } return pd; } // 该方法将在onCreateDialog()调用之后被回调 @Override protected void onPrepareDialog( int id, Dialog dialog) { System. out .println( "-----------------prepare-----------------" ); super.onPrepareDialog(id, dialog); switch (id) { case progress_Dialog: pd.incrementSecondaryProgressBy(-pd.getProgress()); new Thread() { public void run() { while (progressStatus < 100) { progressStatus = doWork(); Message msg = new Message(); msg.what = 0x112; handler.sendMessage(msg); } if (progressStatus >= 100) { pd.dismiss(); // 关闭对话框 } } }.start(); break ; } } private int doWork() { date[hasDate++] = ( int ) (Math.random() * 100); try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } return hasDate; } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true ; } } |
运行效果:
博客内容主要用于日常学习记录,内容比较随意,如有问题,还需谅解!!!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本