浏览 6515 次
锁定老帖子 主题:ProgressDialog 使用
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2010-01-13
最后修改:2010-01-13
[功能] ProgressDialog 也是一种Dialog 一般 在出现ProgressDialog 后台都会再开辟Thread 来做一些耗时的工作 我演示的是从100数到0 这段时间既不太长 不用浪费时间等待 同时 也能明显地看出效果 [代码] 1. ProgressDialog 使用 public void startProgress(){ //to start Progress pd = ProgressDialog.show(this, "loop from 100 to 0!", "is looping...", true, false); //pd.setIcon(R.drawable.icon); } 2. 开辟一个Thread来从100数到0 在数完后 关闭ProgressDialog public class TaskLoop implements Runnable { @Override public void run() { // TODO Auto-generated method stub loop(5000); messageListener.sendEmptyMessage(TASK_LOOP_COMPLETE); } } public void loop(long i){ long j = i; while(j>0){ Log.d("tag",j+""); j = j-1; } } 3. 运行该Thread Thread loop = new Thread(new TaskLoop()); loop.start(); 4.定义一个Handler 用于接受 关闭ProgressDialog 的通知 private Handler messageListener = new Handler(){ public void handleMessage(Message msg) { switch(msg.arg1){ case TASK_LOOP_COMPLETE: pd.dismiss(); break; } } }; [所有代码] public class MyProgressUsage extends Activity { public final static int TASK_LOOP_COMPLETE = 0; ProgressDialog pd; TextView tv; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); findViewById(R.id.start).setOnClickListener(new OnClickListener(){ public void onClick(View v) { // TODO Auto-generated method stub startLoop(); } }); } public void startLoop(){ startProgress(); Thread loop = new Thread(new TaskLoop()); loop.start(); } public class TaskLoop implements Runnable { @Override public void run() { // TODO Auto-generated method stub loop(5000); messageListener.sendEmptyMessage(TASK_LOOP_COMPLETE); } } public void startProgress(){ //to start Progress pd = ProgressDialog.show(this, "loop from 100 to 0!", "is looping...", true, false); //pd.setIcon(R.drawable.icon); } //to do some time-cost task public void loop(long i){ long j = i; while(j>0){ Log.d("tag",j+""); j = j-1; } } private Handler messageListener = new Handler(){ public void handleMessage(Message msg) { switch(msg.arg1){ case TASK_LOOP_COMPLETE: pd.dismiss(); break; } } }; } http://dl.iteye.com/upload/picture/pic/52676/11cd6622-cb13-337a-bd52-007185e972c0.png over! 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |