import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button; public class MainActivity extends Activity {
ProgressDialog dialog=null; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn=(Button) findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
dialog=new ProgressDialog(MainActivity.this);
dialog.setMax(100);//设置最大值
dialog.setTitle("安装进度");
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);//设置进度条样式,水平样式
dialog.show();//显示对话框 /*
* 定义一个线程,重写run()方法
*/
Thread thread=new Thread(){
public void run(){
while(true){
dialog.incrementProgressBy(1);//设置每次增加的进度
/**进度条的值超过100时,让进度条消失*/
if(dialog.getProgress()>=100){
dialog.dismiss();
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
thread.start();//启动线程
}
}); } @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;
} }

dialog的进度条-LMLPHPdialog的进度条-LMLPHP

05-11 22:22