ProgressDialog与线程的整合运用

在后台程序运行时,经常需要弹出进度条,来减少用户的等待感,下面用线程等待3秒来模仿后天程序的运行,简单代码如下:

public class ShowProgressBarActivity extends Activity { private Button mButton1; public ProgressDialog myProgressDialog = null; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mButton1 = (Button) findViewById(R.id.myButton1); mButton1.setOnClickListener(myShowProgressBar); } Button.OnClickListener myShowProgressBar = new Button.OnClickListener() { public void onClick(View arg0) { final CharSequence strDialogTitle = getString(R.string.str_dialog_title); final CharSequence strDialogBody = getString(R.string.str_dialog_body); myProgressDialog = ProgressDialog.show(ShowProgressBarActivity.this, strDialogTitle, strDialogBody, true); new Thread() { public void run() { try { sleep(3000); } catch (Exception e) { e.printStackTrace(); } finally { myProgressDialog.dismiss(); } } }.start(); /* 开始运行运行线程 */ } }; }

使用final可以减少内存的使用空间,提高程序运行的效率。这里需要留意的是ProgressDialog的三个参数:

ProgressDialog(Context ,Title,Message);

转载于:https://www.cnblogs.com/boyupeng/archive/2011/03/15/2028546.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值