以下是我的代码:
public void onClick(View view) {
dialog=new ProgressDialog(view.getContext());
dialog.setCancelable(true);
dialog.setMessage("Initializing Please Wait");
dialog.setTitle("Loading");
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
dialog.setProgress(0);
dialog.setMax(100);
dialog.show();
progressStatus=0;
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
while(progressStatus<10)
{
progressStatus=performTask();
}
/*Dismiss the Progress bar*/
myHandler.post(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
dialog.dismiss();//dismiss the dialog
Toast.makeText(getBaseContext(),msg,Toast.LENGTH_SHORT).show();
progressStatus=0;
myProgress=0;
}
});
}
/* Do some task*/
private int performTask()
{
try {
//---simulate doing some work---
Thread.sleep(1000);
} catch (InterruptedException e)
{
e.printStackTrace();
}
return ++myProgress;
}
}).start();
}
每当我点击按钮,大约5秒后会出现progressdialog。我希望我的ProgressDialog在我点击按钮后出现..紧急情况请任何人可以帮忙吗?提前致谢。
答案 0 :(得分:2)
请执行以下 -
class RemoveAssignedVehicle extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Initializing Please Wait");
pDialog.setTitle("Loading");
pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
progressStatus=0;
pDialog.show();
}
protected String doInBackground(String... args) {
// TODO Auto-generated method stub
while(progressStatus<10)
{
progressStatus=performTask();
}
return "";
}
protected void onPostExecute(String result) {
pDialog.dismiss();
Toast.makeText(getBaseContext(),msg,
Toast.LENGTH_SHORT).show();
progressStatus=0;
myProgress=0;
}