我在单击按钮时调用的方法中编写了以下代码:
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setProgress(0);
progressDialog.setIcon(R.drawable.ic_launcher);
progressDialog.setTitle("Downloading files…");
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setButton(DialogInterface.BUTTON_POSITIVE,"OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton)
{
Toast.makeText(getBaseContext(),
"OK clicked!", Toast.LENGTH_SHORT).show();
}
});
progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton)
{
Toast.makeText(getBaseContext(),
"Cancel clicked!", Toast.LENGTH_SHORT).show();
}
});
new Thread(new Runnable(){
public void run(){
for (int i=1; i<=20; i++) {
try {
//---simulate doing something lengthy---
Thread.sleep(1000);
//---update the dialog---
progressDialog.incrementProgressBy((int)(100/20));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
progressDialog.dismiss();
}
}).start();
}
当我单击按钮时,进度条没有启动。