以下是我的代码:
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 秒后就会出现进度对话框。我希望我的进度对话框在我单击按钮后立即显示。紧急情况请有人帮忙吗?提前致谢。
最佳答案
请执行以下操作 -
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;
}
关于java - ProgressDialog 出现得太晚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24445119/